Topic:
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)
Ideas:
1) Use recursion, sort first, determine the first, then determine the second, and then look for the third one.
Packagesum;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List; Public classThreesum { PublicList<list<integer>> Threesum (int[] nums) {List<List<Integer>> res =NewArraylist<list<integer>>(); intLen; if(Nums = =NULL|| (len = nums.length) < 3)returnRes; Arrays.sort (Nums); for(inti = 0; i < len-2; ++i) {intrem = 0-Nums[i]; List<Integer> subres =NewArraylist<integer>(); Subres.add (Nums[i]); Gettwo (REM, I-W, Nums, Len, Subres, RES); //Move forward If Next element is the same as current element while(I < len-1 && nums[i+1] = = Nums[i]) + +i; } returnRes; } Private voidGettwo (intRemintStartint[] Nums,intLen, list<integer> subres, list<list<integer>>Res) { for(inti = start; i < len-1; ++i) {intLast = REM-Nums[i]; List<Integer> cpysubres =NewArraylist<integer>(Subres); Cpysubres.add (Nums[i]); GetLast (Last, I-W, Nums, Len, Cpysubres, RES); //Move forward If Next element is the same as current element while(I < len-1 && nums[i+1] = = Nums[i]) + +i; } } Private voidGetLast (intRemintStartint[] Nums,intLen, list<integer> subres, list<list<integer>>Res) { for(inti = start; i < Len; ++i) {if(REM = =Nums[i]) {Subres.add (nums[i]); Res.add (Subres); Break; } } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubThreesum T =Newthreesum (); int[] S = {-1, 0, 1, 2,-1,-4 }; List<List<Integer>> res =t.threesum (S); for(list<integer>subres:res) { for(inti:subres) System.out.print (i+ "\ T"); System.out.println ("\ n"); } }}
2) non-recursive, set the first element after the order, and then use two pointers to the remaining elements of the head and tail, a clip on both sides, and then move.
Packagesum;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List; Public classThreesum { PublicList<list<integer>> Threesum (int[] nums) {List<List<Integer>> res =NewArraylist<list<integer>>(); intLen; if(Nums = =NULL|| (len = nums.length) < 3)returnRes; Arrays.sort (Nums); for(inti = 0; I < Len-2;) { intrem = 0-Nums[i]; intleft = i + 1; intright = Len-1; while(Left <Right ) { if(Nums[left] + nums[right] = =REM) {List<Integer> subres =NewArraylist<integer>(); Subres.add (Nums[i]); Subres.add (Nums[left]); Subres.add (Nums[right]); Res.add (Subres); //Filter the duplicated elements. Do{++left;} while(Left < len && Nums[left] = = Nums[left-1]); Do{--right;} while(Right >= 0 && nums[right + 1] = =Nums[right]); } Else if(Nums[left] + Nums[right] <REM) { Left++; } Else{ Right--; } } //Move Forward if the next element is the same as current element. Do{++i;} while(I < len && nums[i] = = Nums[i-1]); } returnRes; } Public Static voidMain (string[] args) {//TODO auto-generated Method StubThreesum T =Newthreesum (); int[] S = { -2,0,1,1,2 }; List<List<Integer>> res =t.threesum (S); for(list<integer>subres:res) { for(inti:subres) System.out.print (i+ "\ T"); System.out.println ("\ n"); } }}
Leetcode-three Sum