1、Java AC
public class Solution { public ArrayList<ArrayList<Integer>> threeSum(int[] num) { ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); int len = num.length; Arrays.sort(num); for (int i = 0; i < len - 2; i++) { if (i > 0 && num[i] == num[i - 1]) { continue; } getTargetNum(list, num, i, i + 1, len - 1, len); } return list; } public void getTargetNum(ArrayList<ArrayList<Integer>> list, int[] num, int first, int low, int high, int len) { int target = num[first] * -1; ArrayList<Integer> numList = new ArrayList<Integer>(); while (low < high) { if (num[low] + num[high] > target) { high--; } else if (num[low] + num[high] < target) { low++; } else { numList.add(num[first]); numList.add(num[low]); numList.add(num[high]); list.add(new ArrayList<Integer>(numList)); numList.clear(); int tempLow = low; int tempHigh = high; low++; high--; while (low < len && num[low] == num[tempLow]) { low++; } while (high >= 0 && num[high] == num[tempHigh]) { high--; } } } } }2、Java AC
public class Solution { public int closet; public int threeSumClosest(int[] num, int target) { int len = num.length; Arrays.sort(num); closet = num[0] + num[1] + num[2]; for (int i = 0; i < len - 2; i++) { if (i > 0 && num[i] == num[i - 1]) { continue; } getTargetNum(num, i, i + 1, len - 1, len, target); } return closet; } public void getTargetNum(int[] num, int first, int low, int high, int len, int target) { int tempValue = 0; while (low < high) { tempValue = num[first] + num[low] + num[high]; if (Math.abs(tempValue - target) < Math.abs(closet - target)) { closet = tempValue; } if (tempValue > target) { int tempHigh = high; high--; while (high >= 0 && num[high] == num[tempHigh]) { high--; } } else if (tempValue < target) { int tempLow = low; low++; while (low < len && num[low] == num[tempLow]) { low++; } } else { return; } } } }3、Java AC
public class Solution { public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) { ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); int len = num.length; Arrays.sort(num); for (int i = 0; i < len - 3; i++) { if (i > 0 && num[i] == num[i - 1]) { continue; } for (int j = i+1; j < len-2; j++) { if (j > i+1 && num[j] == num[j - 1]) { continue; } getTargetNum(list, num, i, j, j + 1, len - 1, len, target); } } return list; } public void getTargetNum(ArrayList<ArrayList<Integer>> list, int[] num, int first, int second, int low, int high, int len, int target) { ArrayList<Integer> numList = new ArrayList<Integer>(); int tempValue = (num[first] + num[second]); while (low < high) { if (tempValue + num[low] + num[high] > target) { high--; } else if (tempValue + num[low] + num[high] < target) { low++; } else { numList.add(num[first]); numList.add(num[second]); numList.add(num[low]); numList.add(num[high]); list.add(new ArrayList<Integer>(numList)); numList.clear(); int tempLow = low; int tempHigh = high; low++; high--; while (low < len && num[low] == num[tempLow]) { low++; } while (high >= 0 && num[high] == num[tempHigh]) { high--; } } } } }