-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution18.java
More file actions
executable file
·38 lines (37 loc) · 1.37 KB
/
Copy pathSolution18.java
File metadata and controls
executable file
·38 lines (37 loc) · 1.37 KB
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
import java.util.*;
class Solution18 {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int a = 0; a < nums.length; a++) {
if (a > 0 && nums[a] == nums[a - 1]) continue; // 去重
for (int b = a + 1; b < nums.length; b++) {
if (b > a + 1 && nums[b] == nums[b - 1]) continue; // 去重
int c = b + 1, d = nums.length - 1;
for (int i = b + 1; i < nums.length; i++) {
if (c >= d) break;
if (c > b + 1 && nums[c] == nums[c - 1]) {
// 去重
c++;
continue;
}
long t = 0l + nums[a] + nums[b] + nums[c] + nums[d];
if (t == target) {
List<Integer> ans = new ArrayList<>();
ans.add(nums[a]);
ans.add(nums[b]);
ans.add(nums[c]);
ans.add(nums[d]);
res.add(ans);
c++;
} else if (t < target) {
c++;
} else {
d--;
}
}
}
}
return res;
}
}