Repository navigation
Expand file tree
/
Copy pathproblem15.java
More file actions
78 lines (52 loc) · 1.56 KB
/
Copy pathproblem15.java
File metadata and controls
78 lines (52 loc) · 1.56 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
77
78
// 15.3 sum
import java.util.*;
class Problem15{
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
Map<Integer,Integer> x= new HashMap<>();
Set<List<Integer>> set = new HashSet<>();
for(int i=0;i<nums.length;i++)
{
x.put(nums[i], i);
}
for(int i=0;i<nums.length;i++)
{
if(i>1 && nums[i-1]==nums[i])
continue;
int c = nums[i];
int target = 0-c;
for(int j=i+1;j<nums.length;j++)
{
if(j>i+1 && nums[j]==nums[j-1])
continue;
int temp= target- nums[j];
if(x.containsKey(temp) && x.get(temp)>j)
{
List<Integer> last = new ArrayList<>();
last.add(nums[i]);
last.add(nums[j]);
last.add(temp);
//ans.add(last);
set.add(last);
}
}
}
ans.addAll(set);
return ans;
}
public static void main(String[] args)
{
int []n={0,0,0,0,0,0};
List<List<Integer>> x=threeSum(n);
for(List<Integer>mylist : x)
{
for(Integer i : mylist)
{
System.err.print(i+",");
}
System.err.println();
}
return;
}
}