-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3Sum.java
More file actions
executable file
·30 lines (30 loc) · 1001 Bytes
/
3Sum.java
File metadata and controls
executable file
·30 lines (30 loc) · 1001 Bytes
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
public class Solution {
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
Arrays.sort(num);
int n = num.length;
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Set<ArrayList<Integer>> resSet = new HashSet<ArrayList<Integer>>();
for(int i = 0; i < n; i++){
int target = -num[i];
int s = i + 1;
int e = n - 1;
while(s < e){
if (num[s] + num[e] == target){
ArrayList<Integer> items = new ArrayList<Integer>();
items.add(num[i]);
items.add(num[s]);
items.add(num[e]);
resSet.add(items);
e--;
s++;
} else if(num[s] + num[e] > target){
e--;
} else{
s++;
}
}
}
res.addAll(resSet);
return res;
}
}