|
| 1 | +## 四数之和 |
| 2 | +### 题目描述 |
| 3 | + |
| 4 | +给定一个包含 n 个整数的数组 nums 和一个目标值 target, |
| 5 | +判断 nums 中是否存在四个元素 a,b,c 和 d , |
| 6 | +使得 a + b + c + d 的值与 target 相等? |
| 7 | +找出所有满足条件且不重复的四元组。 |
| 8 | + |
| 9 | +注意: |
| 10 | + |
| 11 | +答案中不可以包含重复的四元组。 |
| 12 | + |
| 13 | +示例: |
| 14 | + |
| 15 | +给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 |
| 16 | + |
| 17 | +满足要求的四元组集合为: |
| 18 | +[ |
| 19 | + [-1, 0, 0, 1], |
| 20 | + [-2, -1, 1, 2], |
| 21 | + [-2, 0, 0, 2] |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +### 解法 |
| 26 | +1. 将数组排序 |
| 27 | +2. 先假设确定一个数 nums[i] 将 4Sum 问题转换为 3Sum 问题; |
| 28 | +3. 再假设确定一个数将 3Sum 问题转换为 2Sum 问题; |
| 29 | +4. 对排序数组,用首尾指针向中间靠拢的思路寻找满足 target 的 nums[l] 和 nums[k] |
| 30 | + |
| 31 | +```java |
| 32 | +class Solution { |
| 33 | + public List<List<Integer>> fourSum(int[] nums, int target) { |
| 34 | + |
| 35 | + List<List<Integer>> re = new ArrayList<>(); |
| 36 | + if(nums == null || nums.length < 4) { |
| 37 | + return re; |
| 38 | + } |
| 39 | + Arrays.sort(nums); |
| 40 | + for (int i = 0; i < nums.length - 3; i++) { |
| 41 | + |
| 42 | + // 当 nums[i] 对应的最小组合都大于 target 时,后面大于 nums[i] 的组合必然也大于 target, |
| 43 | + if(nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target) { |
| 44 | + break; |
| 45 | + } |
| 46 | + // 当 nums[i] 对应的最大组合都小于 target 时, nums[i] 的其他组合必然也小于 target |
| 47 | + if(nums[i] + nums[nums.length-3] + nums[nums.length-2] + nums[nums.length-1] < target) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + int firstNum = nums[i]; |
| 52 | + for (int j = i + 1; j < nums.length - 2; j++) { |
| 53 | + |
| 54 | + // nums[j] 过大时,与 nums[i] 过大同理 |
| 55 | + if(nums[i] + nums[j] + nums[j+1] + nums[j+2] > target) { |
| 56 | + break; |
| 57 | + } |
| 58 | + // nums[j] 过小时,与 nums[i] 过小同理 |
| 59 | + if(nums[i] + nums[j] + nums[nums.length-2] + nums[nums.length-1] < target) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + int twoSum = target - nums[i] - nums[j]; |
| 64 | + int l = j + 1; |
| 65 | + int k = nums.length - 1; |
| 66 | + while (l < k) { |
| 67 | + int tempSum = nums[l] + nums[k]; |
| 68 | + if(tempSum == twoSum) { |
| 69 | + ArrayList<Integer> oneGroup = new ArrayList<>(4); |
| 70 | + oneGroup.add(nums[i]); |
| 71 | + oneGroup.add(nums[j]); |
| 72 | + oneGroup.add(nums[l++]); |
| 73 | + oneGroup.add(nums[k--]); |
| 74 | + re.add(oneGroup); |
| 75 | + while (l < nums.length && l < k && nums[l] == oneGroup.get(2) && nums[k] == oneGroup.get(3)) { |
| 76 | + l++;k--; |
| 77 | + } |
| 78 | + } |
| 79 | + else if(tempSum < twoSum) { |
| 80 | + l++; |
| 81 | + } |
| 82 | + else { |
| 83 | + k--; |
| 84 | + } |
| 85 | + } |
| 86 | + // 跳过重复项 |
| 87 | + while ((j < nums.length - 2) && (twoSum + nums[i] + nums[j+1] == target)) { |
| 88 | + j++; |
| 89 | + } |
| 90 | + } |
| 91 | + // 跳过重复项 |
| 92 | + while (i < nums.length - 3 && nums[i+1] == firstNum){ |
| 93 | + i++; |
| 94 | + } |
| 95 | + } |
| 96 | + return re; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
0 commit comments