forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0018-4sum.java
33 lines (32 loc) · 1.14 KB
/
0018-4sum.java
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
class Solution {
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int a = 0; a < nums.length-3; a++) {
if (a > 0 && nums[a] == nums[a - 1]) {
continue;
}
for (int i = a + 1; i < nums.length - 2; i++) {
if (i > 1 && nums[i] == nums[i - 1] && i-1 != a) {
continue;
}
int j = i + 1;
int k = nums.length - 1;
while (k > j) {
long sum = (long) nums[i] + nums[j] + nums[k] + nums[a];
if (sum == target) {
res.add(new ArrayList<>(Arrays.asList(nums[a],nums[i], nums[j], nums[k])));
j++;
while (nums[j] == nums[j - 1] && j < k) {
j++;
}
} else if (sum > target) {
k--;
} else
j++;
}
}
}
return res;
}
}