*LeetCode-Two Sum

不知为什么印象中上次是用二分查找做的,但是这次超时,看提交记录发现上次就是用的hashmap

不是二分!是两个指针!

首先对数组按小到大排序,然后设定两个指针head、tail分别指向排序好的数组的首尾:                                     

  • 如果两个指针对应的元素和等于target,那么找到了
  • 如果两个指针对应的元素和小于target,那么需要增加和的大小,则把head指针向后移动
  • 如果两个指针对应的元素和大于target,那么需要减少和的大小,则把tail指针向前移动
  • head赶上tail指针时,结束

时间在找的过程中是o(n)但是排序就是nlogn了,LC原题要求返回index 所以这个方法还是要额外空间。假如只要返回是都有这样的pair

这个方法没有用额外空间。

这个题还要注意的follow up:

1.重复元素都要保存怎么办 比如要输出所有的可能情况

2.有2+2 = 4的情况怎么办。这个的话 以下的hashmap解法就已经可以做到了

hashmap 的key用数组element的值,value用对应的index,这样扫一遍就可以了。关于没有dup key但是数组中有dup 数值,可以看下面的讨论。

hashmap在java中插入是o(1)的,在c++中是o(n)的,所以这个解法在java是o(n)

二分查找写的时候忘记了排序,java array sort用的是merge sort nlogn的,循环加二分,也是nlogn

hashmap,hashtable,hashset区别

http://www.pakzilla.com/2009/08/24/hashmap-vs-hashtable-vs-hashset/

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


关于duplicate:

Your solution is absolute correct. because when duplicate happened, your index of the duplicate in the map is the latest one

Then when you in the second loop, even if the two number is duplicate, but you are right now at the position of first one, the map will return the second one. then it goes correct.

for example {3,6,6,7,3}, 6: after the map initialization, key,3 -> value,5. then you start check from first, when checking the '3', the index is '1', but the map will return the value 5. then the answer is (1,5). correct.

这题后来出了设计数据结构的follow up https://leetcode.com/problems/two-sum-iii-data-structure-design/

记得因为假如有两个1 那么也是可以得到sum 2的 所以用set不可以 需要用map记录一下这个数字里面有几个 多于两个就可以

所以每次要判断是否 n + n =target

public class TwoSum {
    HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
    // Add the number to an internal data structure.
	public void add(int number) {
	    if ( map.containsKey ( number ) )
	        map.put ( number, 2 );
	    else
	        map.put ( number, 1 );
	}

    // Find if there exists any pair of numbers which sum is equal to the value.
	public boolean find(int value) {
	    for ( int i : map.keySet() ){
	        if ( ( 2*i != value && map.containsKey ( value - i ))|| (i*2 == value && map.get( i ) == 2))
	            return true;
	    }
	    return false;
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值