-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.java
33 lines (29 loc) · 1.07 KB
/
3Sum.java
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
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(num.length<3) return res;
Arrays.sort(num);
for(int i=0;i<num.length-2;i++){
if(i==0 || num[i]>num[i-1]){
int j=i+1,
k=num.length-1;
while(j<k){
if(num[j]+num[k]==-num[i]){
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(num[i]);
temp.add(num[j]);
temp.add(num[k]);
res.add(temp);
k--;
j++;
while(k>j && num[k]==num[k+1]) k--;
while(j<k && num[j]==num[j-1]) j++;
}else if(num[j]+num[k]>-num[i]){
k--;
}else{
j++;
}
}
}
}
return res;
}