-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
36 lines (33 loc) · 835 Bytes
/
Copy pathSolution.java
File metadata and controls
36 lines (33 loc) · 835 Bytes
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
package _3sum;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Solution {
public List<List<Integer>> threeSum(int[] num) {
Arrays.sort(num);
List<List<Integer>> result = new LinkedList<>();
for (int i = num.length - 1; i > 1; i--) {
if (i + 1 < num.length && num[i] == num[i + 1]) {
continue;
}
for (int j = 0, k = i - 1; j < k; ) {
int sum = num[j] + num[k] + num[i];
if (sum < 0) {
j++;
} else if (sum > 0) {
k--;
} else {
result.add(Arrays.asList(num[j], num[k], num[i]));
do {
j++;
k--;
} while (j < k && num[j - 1] == num[j] && num[k] == num[k + 1]);
}
}
}
return result;
}
public static void main(String[] args) {
System.out.println(new Solution().threeSum(new int[]{0, 0, 0, 0, 0, 0, 0}));
}
}