题意
寻找三个数a,b,c。使得a+b+c=0。找到所有的不重复的组合。注意[-1,1,0]和[1,-1,0]是重复的。
返回所有组合为0的列表数组。
我的想法
容易想到法子是暴力法。遍历所有的三个子串,然后如果相加的和等于0,加入列表。每次加入列表还需判重。这个效率是O(n^3)显然是会超时的。
1 | public class Solution { |
- 如何优化呢?
大神思路
关于K-sum问题,有一个通解方式使得暴力法的复杂程度O(n^k^)降低到O(n^(k-1)^)。
首先:把整个数组排序(ASC),实现复杂度为O(nlogn)
第二步:按住k-2个数,另外两个数分别从排好序的数组两端找如果大于目标数,右标左移动,反之移动左标。注意要避开之前固定的k-2个数。
- 排序带来最大的好处就是可以帮助去重,如果当前固定的元素和上个已经做了查找的数相同,就可以过滤掉了。而且可以从按住的数的后面开始找。想想是为什么?
下面是我用大神思路实现的代码
优化后代码
1 | public class Solution { |
有了上述思路后,同类题16-3Sum Closest 和 18-4Sum 都可以迎刃而解
16.3Sum Closest题意
和3Sum题目类似,只是这里需要输入一个target,找出最接近target的3个数解。你可以假定这个解有且只存在一个。
16.3Sum Closest我的思路和代码
其实和3Sum类似,只需在查到和target相等时,直接返回。
查到和target不相等时,记录和target之间的差值。找到最小差值。
1 | public class Solution { |
18.4Sum 题意
和3Sum一样,只是从3个数提升到了4个数。不是找到和等于0,而是等于输入的target。
18.4Sum 我的代码
1 | public class Solution { |
16.4Sum 大神代码
在以上的基础上还做了一层优化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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105public List<List<Integer>> fourSum(int[] nums, int target) {
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
int len = nums.length;
if (nums == null || len < 4)
return res;
Arrays.sort(nums);
int max = nums[len - 1];
if (4 * nums[0] > target || 4 * max < target)
return res;
int i, z;
for (i = 0; i < len; i++) {
z = nums[i];
if (i > 0 && z == nums[i - 1])// avoid duplicate
continue;
if (z + 3 * max < target) // z is too small
continue;
if (4 * z > target) // z is too large
break;
if (4 * z == target) { // z is the boundary
if (i + 3 < len && nums[i + 3] == z)
res.add(Arrays.asList(z, z, z, z));
break;
}
threeSumForFourSum(nums, target - z, i + 1, len - 1, res, z);
}
return res;
}
/*
* Find all possible distinguished three numbers adding up to the target
* in sorted array nums[] between indices low and high. If there are,
* add all of them into the ArrayList fourSumList, using
* fourSumList.add(Arrays.asList(z1, the three numbers))
*/
public void threeSumForFourSum(int[] nums, int target, int low, int high, ArrayList<List<Integer>> fourSumList,
int z1) {
if (low + 1 >= high)
return;
int max = nums[high];
if (3 * nums[low] > target || 3 * max < target)
return;
int i, z;
for (i = low; i < high - 1; i++) {
z = nums[i];
if (i > low && z == nums[i - 1]) // avoid duplicate
continue;
if (z + 2 * max < target) // z is too small
continue;
if (3 * z > target) // z is too large
break;
if (3 * z == target) { // z is the boundary
if (i + 1 < high && nums[i + 2] == z)
fourSumList.add(Arrays.asList(z1, z, z, z));
break;
}
twoSumForFourSum(nums, target - z, i + 1, high, fourSumList, z1, z);
}
}
/*
* Find all possible distinguished two numbers adding up to the target
* in sorted array nums[] between indices low and high. If there are,
* add all of them into the ArrayList fourSumList, using
* fourSumList.add(Arrays.asList(z1, z2, the two numbers))
*/
public void twoSumForFourSum(int[] nums, int target, int low, int high, ArrayList<List<Integer>> fourSumList,
int z1, int z2) {
if (low >= high)
return;
if (2 * nums[low] > target || 2 * nums[high] < target)
return;
int i = low, j = high, sum, x;
while (i < j) {
sum = nums[i] + nums[j];
if (sum == target) {
fourSumList.add(Arrays.asList(z1, z2, nums[i], nums[j]));
x = nums[i];
while (++i < j && x == nums[i]) // avoid duplicate
;
x = nums[j];
while (i < --j && x == nums[j]) // avoid duplicate
;
}
if (sum < target)
i++;
if (sum > target)
j--;
}
return;
}