leetcode18 4Sum
# 18. 四数之和 (opens new window)
English Version (opens new window)
# 题目描述
给定一个包含 n 个整数的数组 nums
和一个目标值 target
,判断 nums
中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target
相等?找出所有满足条件且不重复的四元组。
注意:答案中不可以包含重复的四元组。
示例 1:
输入:nums = [1,0,-1,0,-2,2], target = 0 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
示例 2:
输入:nums = [], target = 0 输出:[]
提示:
0 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109
# 解法
“排序 + 双指针”实现。
# Python3
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
res = []
if nums is None or len(nums) < 4:
return res
n = len(nums)
nums.sort()
for i in range(n - 3):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in range(i + 1, n - 2):
if j > i + 1 and nums[j] == nums[j - 1]:
continue
p, q = j + 1, n - 1
while p < q:
if p > j + 1 and nums[p] == nums[p - 1]:
p += 1
continue
if q < n - 1 and nums[q] == nums[q + 1]:
q -= 1
continue
t = nums[i] + nums[j] + nums[p] + nums[q]
if t == target:
res.append([nums[i], nums[j], nums[p], nums[q]])
p += 1
q -= 1
elif t < target:
p += 1
else:
q -= 1
return res
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
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
# Java
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
int n;
if (nums == null || (n = (nums.length)) < 4) {
return Collections.emptyList();
}
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n - 3; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < n - 2; ++j) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int p = j + 1, q = n - 1;
while (p < q) {
if (p > j + 1 && nums[p] == nums[p - 1]) {
++p;
continue;
}
if (q < n - 1 && nums[q] == nums[q + 1]) {
--q;
continue;
}
int t = nums[i] + nums[j] + nums[p] + nums[q];
if (t == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[p], nums[q]));
++p;
--q;
} else if (t < target) {
++p;
} else {
--q;
}
}
}
}
return res;
}
}
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
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
# JavaScript
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
let len = nums.length;
let res = [];
if (len < 4) return [];
nums.sort((a, b) => a - b);
for (i = 0; i < len - 3; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
if (nums[i] + nums[len - 1] + nums[len - 2] + nums[len - 3] < target) continue;
if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
for (j = i + 1; j < len - 2; j++) {
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
let left = j + 1, right = len - 1;
while (left < right) {
if (nums[i] + nums[j] + nums[left] + nums[right] === target) {
res.push([nums[i], nums[j], nums[left], nums[right]]);
while (nums[left] === nums[left + 1]) left++;
left++;
while (nums[right] === nums[right - 1]) right--;
right--;
continue;
} else if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
right--;
continue;
} else {
left++;
continue;
}
}
}
}
return res;
};
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
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
# ...
1
编辑 (opens new window)
上次更新: 2021/10/30, 12:58:38