-
Notifications
You must be signed in to change notification settings - Fork 0
/
n15.java
41 lines (39 loc) · 1.31 KB
/
n15.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class n15 {
public static void main(String[] args) {
}
public List<List<Integer>> threeSum(int[] nums) {
Map<Integer, Integer> val2index = new HashMap<>();
Set<List<Integer>> res = new HashSet<>();
List<List<Integer>> listres = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length; ++i){
val2index.put(nums[i], i);
}
for(int i = 0; i < nums.length; ++i){
for(int j = i + 1; j < nums.length; ++j){
if(val2index.containsKey(0 - nums[i] - nums[j])){
int temp = val2index.get(0 - nums[i] - nums[j]);
if(temp > i && temp > j){
List<Integer> tripule = new ArrayList<>();
tripule.add(nums[i]);
tripule.add(nums[j]);
tripule.add(0 - nums[i] - nums[j]);
res.add(tripule);
}
}
}
}
for (List<Integer> l : res) {
listres.add(l);
}
return listres;
}
}