題目
給出一個有n個整數的數組S,在S中找到三個整數a, b, c,找到所有使得a + b + c = 0的三元組。
樣例
如S = {-1 0 1 2 -1 -4}, 你需要返回的三元組集合的是:
(-1, 0, 1)
(-1, -1, 2)
注意
Java Code
Java Code
Java Code
Python Code
在三元組(a, b, c),要求a <= b <= c。
結果不能包含重復的三元組。
解題:
法一:直接暴力,時間復雜度是O(N3)

public class Solution { /** * @param numbers : Give an array numbers of n integer * @return : Find all unique triplets in the array which gives the sum of zero. */ public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) { // write your code here ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(numbers == null || numbers.length<3) return result; for(int i = 0;i< numbers.length - 2 ; i++){ ArrayList<Integer> path = new ArrayList<Integer>(); for(int j = i+1;j < numbers.length - 1 ; j++){ for(int k = j+ 1 ; k < numbers.length ; k++) if(numbers[i] + numbers[j] + numbers[k] == 0 ){ path = sort3(numbers[i],numbers[j],numbers[k]); if(result.contains(path)==false) result.add(path); } } } return result; } public ArrayList<Integer> sort3(int a,int b,int c){ ArrayList<Integer> path = new ArrayList<Integer>(); if(a>b){ int tmp = a; a = b; b = tmp; } if(c<=a){ path.add(c); path.add(a); path.add(b); }else if(c>=b){ path.add(a); path.add(b); path.add(c); }else{ path.add(a); path.add(c); path.add(b); } return path; } }
在運行到94%的測試數據的時候WA,下面答案只是第三個和第二個的順序不一樣,怎么能就算錯了?
法二:
將原數組存放中ArrayList中,通過判斷 -(nums[i]+nums[j]) 是否在ArrayList中,少了第三個for循環,時間復雜度O(N2)增加了空間發雜度O(N)

public class Solution { /** * @param numbers : Give an array numbers of n integer * @return : Find all unique triplets in the array which gives the sum of zero. */ public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) { // write your code here ArrayList<Integer> path = new ArrayList<Integer>(); ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(numbers == null || numbers.length<3) return result; ArrayList<Integer> nums = new ArrayList<Integer>(); for(int i = 0 ;i < numbers.length ; i++) nums.add(numbers[i]); for(int i = 0;i< nums.size() - 2;i++){ for(int j = i+ 1; j<nums.size() -1;j++){ int sum = -((Integer)nums.get(i) + (Integer)nums.get(j)); if(nums.contains(sum) && nums.indexOf(sum) !=j && nums.indexOf(sum) !=i){ path = sort3(sum,nums.get(i),nums.get(j)); if(result.contains(path)==false) result.add(path); } } } return result; } public ArrayList<Integer> sort3(int a,int b,int c){ ArrayList<Integer> path = new ArrayList<Integer>(); if(a>b){ int tmp = a; a = b; b = tmp; } if(c<=a){ path.add(c); path.add(a); path.add(b); }else if(c>=b){ path.add(a); path.add(b); path.add(c); }else{ path.add(a); path.add(c); path.add(b); } return path; } }
但是運行還是出現上面的情況
法三:
三個數的和基礎還是兩個數的和、兩個數的和||,a+b+c = 0,等價於a+b = -c,這樣就和兩個數的和|| 很類似,當然也可以參考兩個數的和I求解。
先對數組排序,排序后的數組,定義其實節點i,然后對i+1 到len內的所有節點進行兩端遍歷,這里利用二分查找的思想,
設兩端的兩個下標是left 和right ,顯然 sum=nums[i] + nums[left] + nums[right] >0時候 ,right--,小於0的時候left++,等於0的時候就是答案。時間復雜度O(NlogN)

public class Solution { /** * @param numbers : Give an array numbers of n integer * @return : Find all unique triplets in the array which gives the sum of zero. */ public ArrayList<ArrayList<Integer>> threeSum(int[] numbers) { // write your code here ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(numbers == null || numbers.length<3) return result; Arrays.sort(numbers); for(int i = 0;i<numbers.length; i++){ int left = i+ 1; int right = numbers.length - 1; while(left < right){ int sum = numbers[i] + numbers[left] + numbers[right]; ArrayList<Integer> path = new ArrayList<Integer>(); if(sum==0){ path.add(numbers[i]); path.add(numbers[left]); path.add(numbers[right]); if(result.contains(path)==false) result.add(path); left++; right--; }else if(sum>0){ right--; }else{ left++; } } } return result; } }
總耗時: 1094 ms

class Solution: """ @param numbersbers : Give an array numbersbers of n integer @return : Find all unique triplets in the array which gives the sum of zero. """ def threeSum(self, numbers): # write your code here result = [] if numbers == None or len(numbers) < 3: return result numbers.sort() numlen = len(numbers) for i in range(numlen): left = i + 1 right = numlen - 1 while left < right: sum = numbers[i] + numbers[left] + numbers[right] if sum==0: path = [numbers[i],numbers[left],numbers[right]] if path not in result: result.append(path) left +=1 right -=1 elif sum>0: right -=1 else: left +=1 return result
總耗時: 195 ms