-
Notifications
You must be signed in to change notification settings - Fork 0
/
3Sum.java
47 lines (45 loc) · 1.78 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Solution {
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(num == null || num.length < 3){
return result;
}
Arrays.sort(num); // sort the array first
int len = num.length;
for(int i = 0; i < len - 2; i++){
// remove duplicate
//一般比较两个元素是否相等,用num[i] == nums[i-1]这样
if(i != 0 && num[i] == num[i - 1]){ // we need to compare the curr element with previous element
continue;
}
int left = i + 1;
int right = len - 1;
while(left < right){
int sum = num[i] + num[left] + num[right];
if(sum == 0){
ArrayList<Integer> tmp = new ArrayList<Integer>();
tmp.add(num[i]);
tmp.add(num[left]);
tmp.add(num[right]);
result.add(tmp);
left++;
right--;
// when we visit the element inside, it would have duplicate
// we have change left and right, so new left element should compare with the previous one
// we use the while loop to escape the dups
while(left < right && num[left] == num[left-1]){
left++;
}
while(left < right && num[right] == num[right+1]){
right--;
}
}else if(sum < 0){
left++;
}else{
right--;
}
}
}
return result;
}
}