Leetcode0001

直奔主题

题目源自leetcode,题目编号0001

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个
整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回答案。

暴力枚举:快慢指针遍历nums值的各种组合方式,相加后与target比较,相同返回nums下标值。很容易想到。

//暴力枚举  我第一个想法就是暴力枚举,也最简单。别的我也没想起来咋写,别问为什么,问就是菜鸡。
class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]+nums[j]==target){
                    return new int[]{i, j};;
                }
            }
        }
        return null;
    }
}

//leetcode 的官方暴力
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[0];
    }
}

暴力枚举 算法分析 时间复杂度 O(N^2)空间复杂度O(1)
暴力

由于我没想到别的算法,就直接写国内外大神的题解了。

leetcode提供了一个哈希表的方法 时间和空间复杂度都是O(N)的 注意到方法一的时间复杂度较高的原因是寻找 target - x
的时间复杂度过高。因此,我们需要一种更优秀的方法,能够快速寻找数组中是否存在目标元素。如果存在,我们需要找出它的索引。
使用哈希表,可以将寻找 target - x 的时间复杂度降低到从 O(N)O(N)O(N) 降低到 O(1)O(1)O(1)。
这样我们创建一个哈希表,对于每一个 x,我们首先查询哈希表中是否存在 target - x,然后将 x 插入到哈希表中,即可保证不会让 x和自己匹配。
HashMap
看到这里我就想知道这剩下的30%的人,脑回路是多么新奇

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }
}

如下是jdk1.8的containsKey源码分析
HashMap的containsKey方法,内部实际还是根据key去找对应节点,和get方法类似

//传入参数:key值
public boolean containsKey(Object key) 
{
	//调用核心方法getNode,判断是否存在对应节点
	return getNode(hash(key), key) != null;
}

//传入参数:1.根据key散列计算得到的哈希值 2.key值
final Node<K,V> getNode(int hash, Object key) 
{
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    //判断表是否为空,判断key对应的链表节点是否存在
    if ((tab = table) != null && (n = tab.length) > 0&&(first = tab[(n - 1) & hash]) != null) 
    {
    	//判断链表的头部元素是否是key值对应的真实value,对比项:1.hash 2.key值
        if (first.hash == hash && ((k = first.key) == key || (key != null && key.equals(k))))
            return first;
        if ((e = first.next) != null) 
        {
        	//判断是否是红黑树
            if (first instanceof TreeNode)
            	//走红黑树逻辑,从红黑树中获取对应的value
                return ((TreeNode<K,V>)first).getTreeNode(hash, key);
            //遍历链表
            do 
            {
            	//判断链表下个元素是否是key值对应的真实value,对比项:1.hash 2.key值
                if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k))))
                    return e;
            } while ((e = e.next) != null);
        }
    }
    //没有找到对应节点则返回null
    return null;
}

国际站 Java点赞最多的算法
用的还所HashMap,细节不太一样。

class Solution {
	public int[] twoSum(int[] numbers, int target) {
	    int[] result = new int[2];
	    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
	    for (int i = 0; i < numbers.length; i++) {
	        if (map.containsKey(target - numbers[i])) {
	            result[1] = i;
	            result[0] = map.get(target - numbers[i]);
	            return result;
	        }
	        map.put(numbers[i], i);
	    }
	    return result;
	}
}

Star1

个人总结
本题基本上国内外Java用的千篇一律都是HashMap。
暴力枚举很容易想到,但是时间复杂度太高。HashMap想不到是由于对Java集合不熟悉,有待提升Java基础。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨竹菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值