0%

4Sum

题目描述

给定一个整数数组nums和一个整数值target,找出nums中所有的4个数之和等于target的组合。

解题思路

降低时间复杂度的关键在于避免重复地找出这四个数。将4个数之和转化为两组数之和,即外层之和、内层之和。排序肯定是要有的。之后就是外层的两个指针缩小范围,然后内层两个指针向中间走的过程了。

代码

嘤……我放弃了,还是好好学习别人的代码吧……

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
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
int size = nums.size();
vector<vector<int>> res;
if (size < 4) return res;
sort(nums.begin(), nums.end());
int s1 = 0;
int e1 = size - 1;
while (s1 < e1 - 2) {
if (nums[s1] + nums[e1-2] + nums[e1-1] + nums[e1] < target) s1++;
else if (nums[s1] + nums[s1+1] + nums[s1+2] + nums[e1] > target) e1--;
else {
int tmp1 = s1;
int tmp2 = e1;
twoSum(s1, s1+1, e1-1, e1, res, nums, target);
tmp2--;
while (s1 < tmp2 - 2) {
while (nums[tmp2] == nums[tmp2+1]) tmp2--;
twoSum(s1, s1+1, tmp2-1, tmp2, res, nums, target);
tmp2--;
}
tmp1++;
while (tmp1 < e1 - 2) {
while (nums[tmp1] == nums[tmp1-1]) tmp1++;
twoSum(tmp1, tmp1+1, e1-1, e1, res, nums, target);
tmp1++;
}
e1--;
while (s1 < e1 - 2 && nums[e1] == nums[e1+1]) e1--;
s1++;
while (s1 < e1 - 2 && nums[s1] == nums[s1-1]) s1++;
}
}
sort(res.begin(),res.end());
return res;
}

void twoSum(int s1, int s2, int e2, int e1, vector<vector<int>>& res, vector<int>& nums, int target) {
while (s2 < e2) {
if (nums[s1] + nums[s2] + nums[e2] + nums[e1] < target) s2++;
else if (nums[s1] + nums[s2] + nums[e2] + nums[e1] > target) e2--;
else {
vector<int> tmp{nums[s1], nums[s2], nums[e2], nums[e1]};
res.push_back(tmp);
while (s2 < e2 - 1 && nums[s2] == nums[s2+1]) s2++;
s2++;
}
}
return;
}
};