forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.java
More file actions
29 lines (28 loc) · 829 Bytes
/
3Sum.java
File metadata and controls
29 lines (28 loc) · 829 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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> list = new ArrayList<>();
for (int i = 0; i < nums.length && nums[i] <= 0; i++) {
if (i == 0 || nums[i - 1] != nums[i]) {
twoSumHelper(nums, i, list);
}
}
return list;
}
private void twoSumHelper(int[] nums, int i, List<List<Integer>> list) {
int low = i + 1;
int high = nums.length - 1;
while (low < high) {
int sum = nums[i] + nums[low] + nums[high];
if (sum < 0 || (low > i + 1 && nums[low] == nums[low - 1])) {
low++;
}
else if (sum > 0 || (high < nums.length - 1 && nums[high] == nums[high + 1])) {
high--;
}
else {
list.add(Arrays.asList(nums[i], nums[low++], nums[high--]));
}
}
}
}