Repository navigation
Expand file tree
/
Copy pathThreeSum.java
More file actions
65 lines (54 loc) · 2.11 KB
/
Copy pathThreeSum.java
File metadata and controls
65 lines (54 loc) · 2.11 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
package com.dubey.leetcode;
import java.util.*;
public class ThreeSum {
static class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> res = new HashSet<List<Integer>>();
List<Integer> n = new ArrayList<Integer>();
List<Integer> z = new ArrayList<Integer>();
List<Integer> p = new ArrayList<Integer>();
for (Integer num : nums) {
if (num < 0) {
n.add(num);
} else if (num == 0) {
z.add(num);
} else {
p.add(num);
}
}
Set<Integer> N = new HashSet<Integer>(n);
Set<Integer> P = new HashSet<Integer>(p);
for (int i = 0; i < p.size(); i++) {
if (!z.isEmpty()) {
if (N.contains(-1 * p.get(i))) {
res.add(Arrays.asList(-1 * p.get(i), 0, p.get(i)));
}
}
for (int j = i + 1; j < p.size(); j++) {
if (N.contains((p.get(i) + p.get(j)) * -1)) {
List<Integer> result = Arrays.asList(p.get(i), p.get(j), (p.get(i) + p.get(j)) * -1);
Collections.sort(result);
res.add(result);
}
}
}
for (int i = 0; i < n.size(); i++) {
for (int j = i + 1; j < n.size(); j++) {
if (P.contains((n.get(i) + n.get(j)) * -1)) {
List<Integer> result = Arrays.asList(((n.get(i) + n.get(j)) * -1), n.get(i), n.get(j));
Collections.sort(result);
res.add(result);
}
}
}
if (z.size() > 2) {
res.add(Arrays.asList(0, 0, 0));
}
return new ArrayList<List<Integer>>(res);
}
}
public static void main(String[] args) {
int[] nums = {-1, 0, 1, 2, -1, -4};
System.out.println(new Solution().threeSum(nums));
}
}