-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ15.java
More file actions
28 lines (26 loc) · 895 Bytes
/
Q15.java
File metadata and controls
28 lines (26 loc) · 895 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
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new LinkedList<>();
Map<Integer, Integer> map = new HashMap<>();
Arrays.sort(nums);
int length = nums.length;
for(int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for(int i = 0; i < nums.length; i++) {
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for(int j = i + 1; j < nums.length; j++) {
if(j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
int k = - nums[i] - nums[j];
if(map.containsKey(k) && map.get(k) > j) {
result.add(new LinkedList<>(Arrays.asList(nums[i], nums[j], k)));
}
}
}
return result;
}
}