-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_18.java
More file actions
69 lines (65 loc) · 2.32 KB
/
Copy pathP_18.java
File metadata and controls
69 lines (65 loc) · 2.32 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
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
package leetcode.medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class P_18 {
public List<List<Integer>> fourSum(int[] nums, int target) {
final List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
final int n = nums.length;
for (int i = 0; i < n; i++) {
if (i > 0 && nums[i - 1] == nums[i]) { continue; }
for (int j = i + 1; j < n; j++) {
if (j > (i + 1) && nums[j - 1] == nums[j]) { continue; }
int lo = j + 1;
int hi = n - 1;
while (lo < hi) {
if (lo > j + 1 && nums[lo - 1] == nums[lo]) {
lo++;
continue;
}
if (hi < n - 1 && nums[hi + 1] == nums[hi]) {
hi--;
continue;
}
final int sum = nums[i] + nums[j] + nums[lo] + nums[hi];
if (sum > target) {
hi--;
} else if (sum < target) {
lo++;
} else {
res.add(Arrays.asList(nums[i], nums[j], nums[lo], nums[hi]));
hi--;
lo++;
}
}
}
}
return res;
}
public List<List<Integer>> fourSumSet(int[] nums, int target) {
final Set<List<Integer>> res = new HashSet<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 3; i++) {
for (int j = i + 1; j < nums.length - 2; j++) {
int low = j + 1;
int high = nums.length - 1;
while (low < high) {
final int curr = nums[i] + nums[j] + nums[low] + nums[high];
if (curr == target) {
res.add(Arrays.asList(nums[i], nums[j], nums[low], nums[high]));
low++;
high--;
} else if (curr < target) {
low++;
} else {
high--;
}
}
}
}
return new ArrayList<>(res);
}
}