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
22 lines (22 loc) · 688 Bytes
/
3Sum.java
File metadata and controls
22 lines (22 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 && nums[i] <= 0; i++) {
if (i == 0 || nums[i - 1] != nums[i]) {
Set<Integer> set = new HashSet<>();
for (int j = i + 1; j < nums.length; j++) {
int target = -1 * (nums[i] + nums[j]);
if (set.contains(target)) {
result.add(Arrays.asList(nums[i], nums[j], target));
while (j + 1 < nums.length && nums[j] == nums[j + 1]) {
j++;
}
}
set.add(nums[j]);
}
}
}
return result;
}
}