-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0018.java
More file actions
61 lines (45 loc) · 1.66 KB
/
Copy pathLeetCode0018.java
File metadata and controls
61 lines (45 loc) · 1.66 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
/* 4Sum
* Example:
* Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
* A solution set is:[ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
* */
package Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeetCode0018 {
public static void main(String args[]) {
int[] nums = {1, 0, -1, 0, -2, 2};
int target = 0;
System.out.println(fourSum(nums, target));
}
public static List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int N = nums.length;
for (int i = 0; i < N - 3; i++) {
for (int j = i + 1; j < N - 2; j++) {
int k = j + 1;
int l = N - 1;
while (k < l) {
int sum = nums[i] + nums[j] + nums[k] + nums[l];
if (target == sum) {
res.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
//Skip same elements
while (k < l && nums[k] == nums[k + 1]) k++;
while (k < l && nums[l] == nums[l - 1]) l--;
while (i < N - 3 && nums[i] == nums[i + 1]) i++;
while (j < N - 2 && nums[j] == nums[j + 1]) j++;
k++;
l--;
} else if (sum > target) {
l--;
} else {
k++;
}
}
}
}
return res;
}
}