3 Sum
Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0
? Find all unique triplets in the array which gives the sum of zero.
Notice
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
Example
For example, given array S = {-1 0 1 2 -1 -4}
, A solution set is:
(-1, 0, 1)
(-1, -1, 2)
分析:
这题还是很简单的,使用3个指针即可解决问题。关键是里面重复数字的处理需要当心。
1 public class Solution { 2 /** 3 * @param numbers : Give an array numbers of n integer 4 * @return : Find all unique triplets in the array which gives the sum of zero. 5 */ 6 public ArrayList<ArrayList<Integer>> threeSum(int[] num) { 7 ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>(); 8 if(num == null || num.length < 3) { 9 return rst; 10 } 11 Arrays.sort(num); 12 for (int i = 0; i < num.length - 2; i++) { 13 if (i != 0 && num[i] == num[i - 1]) { 14 continue; // to skip duplicate numbers; e.g [0,0,0,0] 15 } 16 17 int left = i + 1; 18 int right = num.length - 1; 19 while (left < right) { 20 int sum = num[left] + num[right] + num[i]; 21 if (sum == 0) { 22 ArrayList<Integer> tmp = new ArrayList<Integer>(); 23 tmp.add(num[i]); 24 tmp.add(num[left]); 25 tmp.add(num[right]); 26 rst.add(tmp); 27 left++; 28 right--; 29 while (left < right && num[left] == num[left - 1]) { // to skip duplicates 30 left++; 31 } 32 while (left < right && num[right] == num[right + 1]) { // to skip duplicates 33 right--; 34 } 35 } else if (sum < 0) { 36 left++; 37 } else { 38 right--; 39 } 40 } 41 } 42 return rst; 43 } 44 }
4Sum
Given an array S of n integers, are there elements a, b, c, andd in S such that a + b + c + d = target?
Find all unique quadruplets in the array which gives the sum of target.
Notice
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
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)
分析:原理同上
1 public class Solution { 2 /** 3 * @param numbers : Give an array numbersbers of n integer 4 * @param target : you need to find four elements that‘s sum of target 5 * @return : Find all unique quadruplets in the array which gives the sum of 6 * zero. 7 */ 8 public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) { 9 ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>(); 10 if(num == null || num.length < 4) { 11 return rst; 12 } 13 Arrays.sort(num); 14 for (int i = 0; i <= num.length - 4; i++) { 15 if (i != 0 && num[i] == num[i - 1]) { 16 continue; // to skip duplicate numbers; e.g [0,0,0,0] 17 } 18 for (int j = i + 1; j <= num.length - 3; j++) { 19 if (j != i + 1 && num[j] == num[j - 1]) { 20 continue; // to skip duplicate numbers; e.g [0,0,0,0] 21 } 22 int left = j + 1; 23 int right = num.length - 1; 24 while (left < right) { 25 int sum = num[left] + num[right] + num[i] + num[j] - target; 26 if (sum == 0) { 27 ArrayList<Integer> tmp = new ArrayList<Integer>(); 28 tmp.add(num[i]); 29 tmp.add(num[j]); 30 tmp.add(num[left]); 31 tmp.add(num[right]); 32 rst.add(tmp); 33 left++; 34 right--; 35 while (left < right && num[left] == num[left - 1]) { // to skip duplicates 36 left++; 37 } 38 while (left < right && num[right] == num[right + 1]) { // to skip duplicates 39 right--; 40 } 41 } else if (sum < 0) { 42 left++; 43 } else { 44 right--; 45 } 46 } 47 } 48 49 } 50 return rst; 51 } 52 }
转载请注明出处: cnblogs.com/beiyeqingteng/