3 Sum leetcode java

愛做飯的小瑩子發表於2014-07-22

題目

Given an array S of n integers, are there elements a, b, c 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, abc)
  • 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)
實現程式碼為:
 1     public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         if(num.length<3||num == null)
 4             return res;
 5         
 6         HashSet<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>();
 7         
 8         Arrays.sort(num);
 9         
10         for(int i = 0; i <= num.length-3; i++){
11             int low = i+1;
12             int high = num.length-1;
13             while(low<high){//since they cannot be the same one, low should not equal to high
14                 int sum = num[i]+num[low]+num[high];
15                 if(sum == 0){
16                     ArrayList<Integer> unit = new ArrayList<Integer>();
17                     unit.add(num[i]);
18                     unit.add(num[low]);
19                     unit.add(num[high]);
20                     
21                     if(!hs.contains(unit)){
22                         hs.add(unit);
23                         res.add(unit);
24                     }
25                     
26                     low++;
27                     high--;
28                 }else if(sum > 0)
29                     high --;
30                  else
31                     low ++;
32             }
33         }
34         
35         return res;
36     }


同時,解決duplicate問題,也可以通過挪動指標來解決判斷,當找到一個合格結果時,將3個加數指標挪動到與當前值不同的地方,才再進行繼續判斷,程式碼如下:
 1     public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
 3         if(num.length<3||num == null)
 4             return res;
 5         
 6         Arrays.sort(num);
 7         
 8         for(int i = 0; i <= num.length-3; i++){
 9             if(i==0||num[i]!=num[i-1]){//remove dupicate
10                 int low = i+1;
11                 int high = num.length-1;
12                 while(low<high){
13                     int sum = num[i]+num[low]+num[high];
14                     if(sum == 0){
15                         ArrayList<Integer> unit = new ArrayList<Integer>();
16                         unit.add(num[i]);
17                         unit.add(num[low]);
18                         unit.add(num[high]);
19                         
20                         res.add(unit);
21                         
22                         low++;
23                         high--;
24                         
25                         while(low<high&&num[low]==num[low-1])//remove dupicate
26                             low++;
27                         while(low<high&&num[high]==num[high+1])//remove dupicate
28                             high--;
29                             
30                     }else if(sum > 0)
31                         high --;
32                      else
33                         low ++;
34                 }
35             }
36         }
37         return res;
38     }

相關文章