-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.java
More file actions
28 lines (27 loc) · 827 Bytes
/
Copy path3Sum.java
File metadata and controls
28 lines (27 loc) · 827 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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int current = 0;
Arrays.sort(nums);
while (current <= nums.length - 3) {
int lo = current + 1;
int hi = nums.length - 1;
if (current == 0 || nums[current - 1] != nums[current]) {
while (lo < hi) {
if (nums[current] + nums[lo] + nums[hi] == 0) {
result.add(Arrays.asList(nums[current], nums[lo++], nums[hi--]));
while (lo < hi && nums[lo] == nums[lo - 1]) {
lo++;
}
} else if (nums[current] + nums[lo] + nums[hi] > 0) {
hi--;
} else if (nums[current] + nums[lo] + nums[hi] < 0) {
lo++;
}
}
}
current++;
}
return result;
}
}