3 Sum leetcode java

题目: 

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

题解
 3 Sum是two Sum的变种,可以利用two sum的二分查找法来解决问题。
本题比two sum增加的问题有:解决duplicate问题,3个数相加返回数值而非index。
首先,对数组进行排序。
然后,从0位置开始到倒数第三个位置(num.length-3),进行遍历,假定num[i]就是3sum中得第一个加数,然后从i+1的位置开始,进行2sum的运算。
当找到一个3sum==0的情况时,判断是否在结果hashset中出现过,没有则添加。(利用hashset的value唯一性)
因为结果不唯一,此时不能停止,继续搜索,low和high指针同时挪动。

时间复杂度是O(n2)

实现代码为:

public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(num.length<3||num == null)
            return res;
        
        HashSet<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>();
        
        Arrays.sort(num);
        
        for(int i = 0; i <= num.length-3; i++){
            int low = i+1;
            int high = num.length-1;
            while(low<high){//since they cannot be the same one, low should not equal to high
                int sum = num[i]+num[low]+num[high];
                if(sum == 0){
                    ArrayList<Integer> unit = new ArrayList<Integer>();
                    unit.add(num[i]);
                    unit.add(num[low]);
                    unit.add(num[high]);
                    
                    if(!hs.contains(unit)){
                        hs.add(unit);
                        res.add(unit);
                    }
                    
                    low++;
                    high--;
                }else if(sum > 0)
                    high --;
                 else
                    low ++;
            }
        }
        
        return res;
    }

同时,解决duplicate问题,也可以通过挪动指针来解决判断,当找到一个合格结果时,将3个加数指针挪动到与当前值不同的地方,才再进行继续判断,代码如下:
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(num.length<3||num == null)
            return res;
        
        Arrays.sort(num);
        
        for(int i = 0; i <= num.length-3; i++){
            if(i==0||num[i]!=num[i-1]){//remove dupicate
                int low = i+1;
                int high = num.length-1;
                while(low<high){
                    int sum = num[i]+num[low]+num[high];
                    if(sum == 0){
                        ArrayList<Integer> unit = new ArrayList<Integer>();
                        unit.add(num[i]);
                        unit.add(num[low]);
                        unit.add(num[high]);
                        
                        res.add(unit);
                        
                        low++;
                        high--;
                        
                        while(low<high&&num[low]==num[low-1])//remove dupicate
                            low++;
                        while(low<high&&num[high]==num[high-1])//remove dupicate
                            high--;
                            
                    }else if(sum > 0)
                        high --;
                     else
                        low ++;
                }
            }
        }
        return res;
    }
Refrence:http://www.cnblogs.com/springfor/p/3859670.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值