-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN15ThreeSum.java
96 lines (82 loc) · 2.46 KB
/
N15ThreeSum.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
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
85
86
87
88
89
90
91
92
93
94
95
96
package LeetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class N15ThreeSum {
static List<List<Integer>> solution(int[] nums){
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for(int i=0;i<nums.length-2;i++){
// 剪枝
if(nums[i]>0){
return result;
}
int left = i+1;
int right = nums.length-1;
while (left<right){
if(nums[left]+nums[right]+nums[i]==0){
result.add(Arrays.asList(nums[i],nums[left],nums[right]));
// no break
while (left<right && nums[left]==nums[left+1]){
left++;
}
while (left<right && nums[right]==nums[right-1]){
right--;
}
left++;
right--;
}else if(nums[left]+nums[right]+nums[i]<0){
left++;
}else right--;
}
while(i+1<nums.length && nums[i]==nums[i+1]){
i++;
}
}
return result;
}
// faster
static List<List<Integer>> solution2(int[] nums){
int L;
int R;
Arrays.sort(nums);
ArrayList result= new ArrayList();
if(nums.length<3)
return result;
for(int k = 0 ;k<nums.length-2;k++){
if(nums[k]>0)
return result;
L = k+1;
R = nums.length-1;
while (true){
if(nums[k]+nums[L]+nums[R]==0){
result.add(Arrays.asList(nums[k],nums[L],nums[R]));
while(L<R&&nums[L]==nums[L+1]){
L++;
}
while(L<R&&nums[R]==nums[R-1]){
R--;
}
L++;
R--;
}
else if(nums[k]+nums[L]+nums[R]>0){
R--;
}else{
L++;
}
if(L>=R){
break;
}
}
while(k<nums.length-2 && nums[k]==nums[k+1]){
k++;
}
}
return result;
}
public static void main(String[] args) {
int nums[] = {0,0,0};
System.out.println(solution2(nums));
}
}