-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.java
More file actions
76 lines (66 loc) · 2.36 KB
/
Copy path3sum.java
File metadata and controls
76 lines (66 loc) · 2.36 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Time Limit exceeds
class Solution {
public static <Integer> boolean listEqualsIgnoreOrder(List<Integer> list1, List<Integer> list2) {
return new HashSet<>(list1).equals(new HashSet<>(list2));
}
public List<List<Integer>> threeSum(int[] nums) {
int [][] tem = new int [nums.length][nums.length];
for(int i=0;i<nums.length;i++)
{
for(int j=0;j<nums.length;j++)
{
tem[i][j] = nums[i]+nums[j];
}
}
HashMap<Integer,ArrayList<Integer>> h = new HashMap<Integer,ArrayList<Integer>>();
for(int i=0;i<nums.length;i++)
{
ArrayList<Integer> c;
if(h.containsKey(nums[i]))
{
c = h.get(nums[i]);
}
else
c= new ArrayList<Integer>();
c.add(i);
h.put(nums[i],c);
}
List<List<Integer>> result =new ArrayList<List<Integer>>();
for(int i=0;i<nums.length;i++)
{
for(int j=0;j<nums.length;j++)
{
if(j==i)
break;
int t ;
t= tem[i][j];
int ch = t*-1;
if(h.containsKey(ch) && i!=j )
{
ArrayList<Integer> c = h.get(ch);
for(int e=0;e<c.size();e++)
{
int key = c.get(e);
if(key!=i && key!=j)
{
List<Integer> store = new ArrayList<Integer>();
store.add(nums[i]);
store.add(nums[j]);
store.add(nums[key]);
int flg=0;
for(int ko=0;ko<result.size();ko++)
{
List<Integer> koc= result.get(ko);
if(listEqualsIgnoreOrder(koc,store))
{flg=1; break;}
}
if(flg==0)
result.add(store);
}
}
}
}
}
return result;
}
}