problem:
Given an array S of n integers, is there elements a, b, C, and D in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
- Elements in a quadruplet (a,b,c,D) must is in non-descending order. (ie, a ≤ b ≤ c ≤ d)
- The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0-1 0-2 2}, and target = 0. A solution set is: ( -1, 0, 0, 1) ( -2,-1, 1, 2) ( -2, 0, 0, 2)
Analysis:
This problem share the same idea with 2sum, 3sum. (Note ask the index, but the combination) It involve the test aginst your skills in avoding duplicates. Once thing you should keep in mind:the duplicate does not mean the same value appear at different index more than one time s, but mean the same value appear on the same index more than one time. ---------------------------------------------------------------------------Case : CaseA:1 1 2 2 ... This would is not cause duplicate. CaseB:1 (1) .... This would cause duplicate. the CaseB is the situation we should exclude, but we should incldue CaseA.---------------------------------------------------------------------------Arrays.sort (nums); The classic exlude duplicates on the same index is to take advantage of sorted array. Arrays.sort (nums);1. Avoid duplicates at first element (item[0]).if(i = = 0 | | nums[i]! = nums[i-1]) Note:nums[i]! = Nums[i-1] is a very classic method.2. Avoid duplicates at second element (item[1]).if(j = = I+1 | | nums[j]! = nums[j-1]) note:why We can start from I+1, cause CaseA!!! 3. Avoid the duplicate at third element (item[2]) Front++; while(Front < nums.length && Nums[front] = = Nums[front-1]) {Front++;} Note:the above is a very great the skill in the sum, when using front and pointer. 4. Avoid the duplicate at Fourth element (item[3]) You can see there is obvious code forAvoding the last index's duplicate at 2sum, 3sum, and 4sum. The reason is, once we find out the answer, we immediately move front to the next element with different value. Since Nums[front] + nums[end] =Target.nums[new_front]+ Nums[end] must not equal to target. This guarantee there is no duplicates forTHE last index!!!if(Nums[front] + nums[end] = =target) {... front++; while(Front < nums.length && Nums[front] = = Nums[front-1]) {Front++; }}
Solution:
Public classSolution { PublicList<list<integer>> Foursum (int[] Nums,inttarget) { if(Nums = =NULL) Throw NewIllegalArgumentException ("Nums reference is null"); List<List<Integer>> ret =NewArraylist<list<integer>> (); if(Nums.length <= 3) returnret; Arrays.sort (Nums); for(inti = 0; I <= nums.length-4; i++) { for(intj = i+1; J <= Nums.length-3; J + +) { if(i = = 0 | | nums[i]! = nums[i-1]) { if(j = = I+1 | | nums[j]! = nums[j-1]) {twosum (Nums, Nums[i], Nums[j], J+1, target-nums[i]-Nums[j], ret); } } } } returnret; } Private voidTwosum (int[] Nums,intNUM1,intNUM2,intFrontintTarget, list<list<integer>>ret) { intEnd = Nums.length-1; while(Front <end) { if(Nums[front] + nums[end] = =target) {ArrayList<Integer> item =NewArraylist<integer> (); Item.add (NUM1); Item.add (NUM2); Item.add (Nums[front]); Item.add (Nums[end]); Ret.add (item); Front++; while(Front < nums.length && Nums[front] = = Nums[front-1]) {Front++; } } Else if(Nums[front] + Nums[end] <target) {Front++; } Else{End--; } } }}
[Leetcode#18]4sum