zoukankan      html  css  js  c++  java
  • 3sum

    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)
    实现代码为:
     
    解法一:two pointers
    public class Solution {
    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;
        }
    }

     reference:http://www.cnblogs.com/springfor/p/3859670.html

    解法二:hashmap

    public class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
            if(nums==null||nums.length<3)
            {
                return res;
            }
            Arrays.sort(nums);
            HashSet<List<Integer>> set = new HashSet<List<Integer>>();
            for(int i = 0; i<=nums.length-3;i++)
            {
                if(i==0||nums[i]!=nums[i-1])
                {
                    int target = -nums[i];
                    HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
                    for(int j = i+1;j<=nums.length-1;j++)
                    {
                        
                        if(map.containsKey(target-nums[j]))
                        {
                            ArrayList<Integer> unit = new ArrayList<Integer>();
                            unit.add(nums[i]);
                            unit.add(target-nums[j]);
                            unit.add(nums[j]);
                            if(set.add(unit))res.add(unit);
                        }
                        else
                        {
                            map.put(nums[j],1);
                        }
                        
                    }
                }
            }
            return res;
        }
    }
  • 相关阅读:
    JDBC数据库访问操作的动态监测 之 p6spy
    ZeroC Ice启用SSL通讯的配置
    heartbeat在yum系发行版本的处理资料
    VisualStudio 调试Linux
    JVM之上的语言小集
    程序人生【一些经典的资料】
    读史知今、以史为鉴 【技术商业化】
    ipython notebook 浏览器中编写数学公式和现实
    大数据 云计算 等搜集的资料
    GO 1.5 代码编译安装 [centos7 64位]
  • 原文地址:https://www.cnblogs.com/hygeia/p/5469695.html
Copyright © 2011-2022 走看看