-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSolution0015.java
71 lines (67 loc) · 2.21 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 15. 三数之和
/*
数组排序后,先固定一个数,再用双指针从头尾向内移动确定另外两个数,使用HashSet对结果去重
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
Set<List<Integer>> set = new HashSet<>();
Arrays.sort(nums);
for (int i = 0; i < n - 2; i++) {
int l = i + 1, r = n - 1;
while (l < r) {
if (nums[i] + nums[l] + nums[r] == 0) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[l]);
list.add(nums[r]);
set.add(list);
l++;
r--;
} else if (nums[i] + nums[l] + nums[r] < 0) {
l++;
} else {
r--;
}
}
}
List<List<Integer>> res = new ArrayList<>(set);
return res;
}
}
/*
逻辑同上,有优化:
1、遍历时第一个数跟前一个相同,则跳过重复处理
2、使用ArrayList存放结果,当有一组满足条件的三元组时,通过while循环移动指针进行去重,避免出现重复的三元组
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < n - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int l = i + 1, r = n - 1;
while (l < r) {
if (nums[i] + nums[l] + nums[r] == 0) {
res.add(Arrays.asList(nums[i], nums[l], nums[r]));
l++;
r--;
while (l < r && nums[l] == nums[l - 1]) {
l++;
}
while (l < r && nums[r] == nums[r + 1]) {
r--;
}
} else if (nums[i] + nums[l] + nums[r] < 0) {
l++;
} else {
r--;
}
}
}
return res;
}
}