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
21 lines (21 loc) · 704 Bytes
/
3Sum.java
File metadata and controls
21 lines (21 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> result = new HashSet<>();
Set<Integer> duplicates = new HashSet<>();
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (duplicates.add(nums[i])) {
for (int j = i + 1; j < nums.length; j++) {
int target = -nums[i] - nums[j];
if (map.containsKey(target) && map.get(target) == i) {
List<Integer> temp = Arrays.asList(nums[i], nums[j], target);
Collections.sort(temp);
result.add(temp);
}
map.put(nums[j], i);
}
}
}
return new ArrayList<>(result);
}
}