1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class FourSum { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if (nums == null || nums.length < 4) { return result; } Arrays.sort(nums); int l = nums.length; for (int i = 0; i < l - 3; i++) { if (i > 0 && nums[i] == nums[i - 1]) { continue; } for (int j = i + 1; j < l - 2; j++) { if (j > i + 1 && nums[j] == nums[j - 1]) { continue; } for (int k = j + 1; k < l - 1; k++) { if (k > j + 1 && nums[k] == nums[k - 1]) { continue; } int left = target - nums[i] - nums[j] - nums[k]; if (helper(k + 1, l - 1, nums, left)) { List<Integer> r = new ArrayList<Integer>(); r.add(nums[i]); r.add(nums[j]); r.add(nums[k]); r.add(left); result.add(r); } } } } return result; } private boolean helper(int start, int end, int[] nums, int target) { while (start <= end) { int middle = (start + end) / 2; if (nums[middle] == target) { return true; } if (nums[middle] > target) { end = middle - 1; } else { start = middle + 1; } } return false; } }
|