-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreeSum.java
More file actions
56 lines (44 loc) · 1.46 KB
/
Copy pathThreeSum.java
File metadata and controls
56 lines (44 loc) · 1.46 KB
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
package org.example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class ThreeSum {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
// 如何对nums做排序
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (nums[i]>0) {
break;
}
// 去重
if (i != 0 && nums[i] == nums[i - 1]) {
continue;
}
int remain = 0 - nums[i];
// 其 remain 只能在右边找 ?
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
if (nums[left] + nums[right] == remain) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[left]);
list.add(nums[right]);
result.add(list);
// 去重
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] < remain) {
left++;
} else {
right--;
}
}
}
return result;
}
}