-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution0015.java
47 lines (40 loc) · 1.2 KB
/
Solution0015.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
package leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author xiangdotzhaoAtwoqutechcommacom
* @date 2019/12/15
*/
public class Solution0015 {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int low = i + 1;
int hi = nums.length - 1;
int sum = -nums[i];
while (low < hi) {
if (nums[low] + nums[hi] == sum) {
res.add(Arrays.asList(nums[i], nums[low], nums[hi]));
while (low < hi && nums[low] == nums[low + 1]) {
low++;
}
while (low < hi && nums[hi] == nums[hi - 1]) {
hi--;
}
low++;
hi--;
} else if (nums[low] + nums[hi] > sum) {
hi--;
} else {
low++;
}
}
}
return res;
}
}