-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.三数之和.java
61 lines (55 loc) · 1.76 KB
/
15.三数之和.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
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/*
* @lc app=leetcode.cn id=15 lang=java
*
* [15] 三数之和
*/
// @lc code=start
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
return threeSumTwoPointers(nums);
}
// from leetcode official
public List<List<Integer>> threeSumTwoPointers(int[] nums) {
List<List<Integer>> ans = new LinkedList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0) {
break;
} else if (i > 0 && nums[i - 1] == nums[i]) {
// for removing duplicates in answer
continue;
} else {
int j = i + 1, k = nums.length - 1;
// use two pointers to find all (j, k)
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum < 0) {
j++;
} else if (sum > 0) {
k--;
} else {
ans.add(Arrays.asList(nums[i], nums[j], nums[k]));
// this is a shortcut
if (nums[j] == nums[k]) {
break;
}
// for removing duplicates in answer
while (j < k && nums[j] == nums[j + 1]) {
j++;
}
j++;
while (j < k && nums[k - 1] == nums[k]) {
k--;
}
k--;
}
}
}
}
return ans;
}
}
// @lc code=end