- 3Sum:题目链接
基本思想:
先对数组排序 ,用一个for循环寻找第一个元素(索引为i),在用双指针在[i+1,nums.length-1]寻找第二个元素(索引left),第三个元素(索引right) .
如果nums[i] + nums[left] + nums[right] == 0 说明找到了
如果nums[i] + nums[left] + nums[right] < 0 left++
如果nums[i] + nums[left] + nums[right] > 0 right–
方法1:
注意此方法超时了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
39import java.util.*;
public class Solution1 {
// 时间复杂度O(Nlog2^N) + O(N^2) 排序 + 双指针
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; ++i) {
int left = i + 1;
int right = nums.length - 1;
// 在[i+1,nums.length-1] 用双指针搜索另外两个元素
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
List<Integer> list = new ArrayList<>();
list.add(nums[i]);
list.add(nums[left++]);
list.add(nums[right--]);
res.add(list);
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
Set<List<Integer>> set = new HashSet<>(res); // set用来给结果 去重
List<List<Integer>> ans = new ArrayList<>(set); // 将set转list
return res;
}
public static void main(String[] args) {
int[] nums = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> res = new Solution1().threeSum(nums);
System.out.println(res);
}
}
方法2:sorting + twoPoint(推荐)
1 | import java.util.ArrayList; |
pS:
源代码链接