-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.java
More file actions
84 lines (71 loc) · 2.28 KB
/
3Sum.java
File metadata and controls
84 lines (71 loc) · 2.28 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
79
80
81
82
83
84
class Solution {
class Tuple {
private int a;
private int b;
private int c;
public Tuple(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Tuple tuple = (Tuple) o;
return this.a == tuple.a && this.b == tuple.b && this.c == tuple.c;
}
@Override
public int hashCode() {
return Objects.hash(a, b, c);
}
boolean illegal(Map<Integer, Integer> num2Count) {
int count = (a == c ? 1 : 0) + (b == c ? 1 : 0) + 1;
return count > num2Count.getOrDefault(c, 0);
}
List<Integer> toList() {
List<Integer> list = new ArrayList<>();
list.add(a);
list.add(b);
list.add(c);
return list;
}
}
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList();
Map<Integer, Integer> num2Count = new HashMap();
for (int i = 0; i < nums.length; i++) {
num2Count.put(nums[i], num2Count.getOrDefault(nums[i], 0) + 1);
}
Set<Tuple> tupleSet = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
int a = nums[i];
int b = nums[j];
if (a > b) {
int tmp = a;
a = b;
b = tmp;
}
int c = -a - b;
if (c < b) {
continue;
}
Tuple tuple = new Tuple(a, b, c);
if (tupleSet.contains(tuple)) {
continue;
}
if (tuple.illegal(num2Count)) {
continue;
}
tupleSet.add(tuple);
result.add(tuple.toList());
}
}
return result;
}
}