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