-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum_313_cases.java
More file actions
52 lines (43 loc) · 1.63 KB
/
Copy path3sum_313_cases.java
File metadata and controls
52 lines (43 loc) · 1.63 KB
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
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
HashMap<Integer,ArrayList<Integer>> h = new HashMap<Integer,ArrayList<Integer>>();
for(int i=0;i<nums.length;i++)
{
ArrayList<Integer> t = h.containsKey(nums[i]) ? h.get(nums[i]):new ArrayList<Integer>();
t.add(i);
h.put(nums[i],t);
}
List<List<Integer>> res = new ArrayList<List<Integer>>();
Set<ArrayList<Integer>> s = new HashSet<ArrayList<Integer>>();
for(int i=0;i<nums.length;i++)
{
int t =nums[i],tp=i;
for(int j=0;j<nums.length;j++)
{
if(j==tp)
continue;
int find = (t+nums[j])*(-1);
if(h.containsKey(find))
{
ArrayList<Integer> pos = h.get(find);
for(int k=0;k<pos.size();k++)
{
int kget = pos.get(k);
if(kget!=j && kget!=i)
{
ArrayList<Integer> tem = new ArrayList<Integer>();
tem.add(nums[kget]);
tem.add(nums[j]);
tem.add(nums[i]);
Collections.sort(tem);
s.add(tem);
}
}
}
}
}
// System.out.println(s);
res.addAll(s);
return res;
}
}