FourSum

四数之和

题目介绍

四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,**b,cd ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例 :

1
2
3
4
5
6
7
8
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

题目解法

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FourSum {

public static List<List<Integer>> fourSum(int[] nums, int target) {

List<List<Integer>> lists = new ArrayList<>();
if (nums == null || nums.length < 4) {
return lists;
}
Arrays.sort(nums);
int length = nums.length;
for (int i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
break;
}
if (nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
continue;
}
for (int j = i + 1; j < nums.length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
break;
}
if (nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target) {
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
List<Integer> list = Arrays.asList(nums[i], nums[j], nums[left], nums[right]);
lists.add(list);
// 这一段优化很精妙
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
right--;
} else if (sum > target) {
right--;
} else {
left++;
}
}
}
}
return lists;
}

public static void main(String[] args) {

int[] nums = new int[]{-3, -2, -1, 0, 0, 1, 2, 3};
int target = 0;

System.out.println(fourSum(nums, target));
}
}

打印:

1
[[-3, -2, 2, 3], [-3, -1, 1, 3], [-3, 0, 0, 3], [-3, 0, 1, 2], [-2, -1, 0, 3], [-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]

思路:

双指针。其实做出来后,虽然pass了,但是发现效率比较低。所以还是参考了官方的解答,最后一层循环的优化,确实比较厉害。


FourSum
https://yangtzeshore.github.io/2021/01/22/FourSum/
作者
Chen Peng
发布于
2021年1月22日
许可协议