-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
48 lines (45 loc) · 1.45 KB
/
Solution.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
package _015;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/10/14
* desc :
* </pre>
*/
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
int len = nums.length;
if (len < 3) return list;
Arrays.sort(nums);
int max = nums[len - 1];
if (max < 0) return list;
for (int i = 0; i < len - 2; ) {
if (nums[i] > 0) break;
if (nums[i] + 2 * max < 0) {
while (nums[i] == nums[++i] && i < len - 2) ;
continue;
}
int left = i + 1, right = len - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
list.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (nums[left] == nums[++left] && left < right) ;
while (nums[right] == nums[--right] && left < right) ;
} else if (sum < 0) ++left;
else --right;
}
while (nums[i] == nums[++i] && i < len - 2) ;
}
return list;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.threeSum(new int[]{-1, 0, 1, 2, -1, -4}));
}
}