-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum
More file actions
26 lines (23 loc) · 751 Bytes
/
3sum
File metadata and controls
26 lines (23 loc) · 751 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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> res = new HashSet<>();
if (nums.length == 0)
return new ArrayList<>(res);
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
int sum = nums[j] + nums[k];
if (sum == -nums[i]) {
res.add(Arrays.asList(nums[i], nums[j++], nums[k--]));
} else if (sum > -nums[i]) {
k--;
} else if (sum < -nums[i]) {
j++;
}
}
}
return new ArrayList<>(res);
}
}