3 Sum leetcode java

来源:互联网 发布:mac os x怎么更新 编辑:程序博客网 时间:2024/05/15 01:38

题目: 

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魔兽世界怀旧服工程学攻略 魔兽世界怀旧服工程学 北京大学化学与分子工程学院 大连理工大学建设工程学部 北大化学与分子工程学院 工程宝 西安轩宝清洗工程有限公司 工程宝使用方法图解 工程宝的使用方法 工程宝app 中铁北京工程局 中铁广州工程局 广西水电工程局 中铁北京工程局集团有限公司 山东省水利工程局有限公司 中铁上海工程局集团有限公司 中铁上海工程局本科生待遇2018 中交机电工程局有限公司 中铁北京工程局应届生待遇 工程局郑建林被停职 中国建筑第八工程局 中国建筑第五工程局有限公司 中国建筑第五工程局 中国建筑第八工程局有限公司 中国建筑第二工程局 中国建筑第四工程局有限公司 中国建筑第二工程局有限公司 中国建筑第七工程局 中国建筑第四工程局 中国建筑第七工程局有限公司 中国建筑第三工程局有限公司 中国建筑第六工程局有限公司 中铁有多少个工程局 中国水利水电第三工程局 中国水利水电第七工程局 中国水利水电第十一工程局 中国水利水电第六工程局 水利水电第十一工程局 中国水利水电第五工程局 中国水利水电第十四工程局 工程师考试时间