-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.java
More file actions
71 lines (57 loc) · 1.82 KB
/
Copy path3sum.java
File metadata and controls
71 lines (57 loc) · 1.82 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
class Solution {
private Set<ArrayList<Integer>> set;
private int minNum;
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
if (nums.length != 0) {
minNum = nums[0];
}
this.set = new HashSet<ArrayList<Integer>>();
for (int i = 0; i < nums.length; i++) {
twoSum(nums, nums[i], i);
}
return new ArrayList<>(set);
}
public ArrayList<ArrayList<Integer>> twoSum(int[] nums, int target, int numOut) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (i != numOut) {
if (map.containsKey(nums[i])) {
sort(-target - nums[i], nums[i], target);
} else {
int aux = -target - nums[i];
if (aux < minNum && nums[i] > 0 && target > 0) {
break;
}
map.put(aux, 0);
}
}
}
return arrayList;
}
public void sort(int x, int y, int z) {
int max = z;
if (x > max || y > max) {
if (x > y) {
max = x;
} else {
max = y;
}
}
int min = z;
if (x < min || y < min) {
if (x < y) {
min = x;
} else {
min = y;
}
}
int mid = x + y + z - max - min;
ArrayList<Integer> solution = new ArrayList<Integer>();
solution.add(min);
solution.add(mid);
solution.add(max);
set.add(solution);
}
}