-
Notifications
You must be signed in to change notification settings - Fork 0
/
15. 3Sum
44 lines (37 loc) · 1.21 KB
/
15. 3Sum
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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int a = 0;
ArrayList<List<Integer>> result = new ArrayList<>();
while (a <= nums.length - 3) {
int l = a + 1;
int r = nums.length - 1;
while (l < r) {
int total = nums[a] + nums[l] + nums[r];
if (total < 0) {
l++;
}
else if (total > 0) {
r--;
}
else {
result.add(new ArrayList<>(Arrays.asList(nums[a], nums[l], nums[r])));
System.out.println(a + " " + l + " " + r);
l++;
r--;
while (l < nums.length && nums[l] == nums[l-1]) {
l++;
}
while (r > a && nums[r] == nums[r+1]) {
r--;
}
}
}
a++;
while (a <= nums.length - 2 && nums[a] == nums[a-1]) {
a++;
}
}
return result;
}
}