Given an array S of n integers, is 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 is 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)
Solution 1: First think of brute force, O (n^3) time complexity, bound to time Limit exceeded.
classSolution { Public: Vector< vector<int> > Threesum (vector<int>&nums) { intn =nums.size (); Vector< vector<int> >Res; if(N <3) returnRes; for(inti =0; I < n; i++) { for(intj = i +1; J < N; J + +) { for(intK = j +1; K < n; k++) { if(Nums[i] + nums[j] + nums[k] = =0) {vector<int> VI =Adjust (nums[i], nums[j], nums[k]); Vector< vector<int> >::iterator iter =Find (Res.begin (), Res.end (), vi); if(iter = =res.end ()) Res.push_back (vi); } } } } returnRes; }Private: Vector<int> Adjust (int& A,int& B,int&c) {if(A >b) Swap (A, b); if(A >c) {Swap (A, c); Swap (b, c); } Else { if(B >c) Swap (b, c); } Vector<int>VI; Vi.push_back (a); Vi.push_back (b); Vi.push_back (c); returnVI; }};
Solution 2: Assume that the goal of the 3sum problem is target. Each time a number k is selected from the array, the target is equal to the 2sum problem of target-k from the remaining number. It is important to note that there is a small trick: when we select number I from the array, we only need to ask for the 2sum problem of the number of words from i+1 to the last range. Assuming that the array is a[], there are a total of n elements a1,a2 .... An. Obviously, when the A1 is elected, we seek the 2sum problem of the target bit target-a1 in the sub-array [A2~an], we have to prove that when the A2 is elected, we only need to calculate the 2sum problem of the target bit a3~an in the Subarray [TARGET-A2], instead of the sub-array [A1, A3~an], the proof is as follows:
Suppose that in the 2sum problem of the sub-array [A1,a3~an] target bit target-a2, there is A1 + M = target-a2 (M is a number in the A3~an), that is, A2 + M = target-a1, which is exactly "for sub-arrays [A2~an], A solution to the 2sum problem of TARGET-A1. That is, we are equivalent to the three number a1+a2+m = target to meet the 3sum to repeat the calculation. Therefore, in order to avoid repetition, in the sub-array [A1,a3~an], you can remove the A1, and then calculate the target is target-a2 2sum problem.
classSolution { Public: Vector< vector<int> > Threesum (vector<int>&nums) { intn =nums.size (); Vector< vector<int> >Res; if(N <3) returnRes; Sort (Nums.begin (), Nums.end ()); for(inti =0; I < n; i++) { if(i = =0|| (I >0&& nums[i]! = nums[i-1])) { intleft = i +1, right = N-1; intsum =0-Nums[i]; while(Left <Right ) { if(Nums[left] + nums[right] = =sum) {Vector<int>VI; Vi.push_back (Nums[i]); Vi.push_back (Nums[left]); Vi.push_back (Nums[right]); Res.push_back (vi); while(Left < right && nums[left] = = Nums[left +1]) left++; while(Left < right && nums[right] = = Nums[right-1]) right--; Left++; Right--; } Else if(Nums[left] + Nums[right] <sum) left++; Else Right--; } } } returnRes; }};
Reference: http://www.cnblogs.com/tenosdoit/p/3649607.html
[Leetcode]21. 3Sum and the sum of the three