Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

1
2
3
4
5
6
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

3sum的最优解时间复杂度是O(N2)的,4sum如果建立在3sum之上,最优解的时间复杂度是O(N3),实现代码如下

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
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
int s = nums.size();
if (s < 4) {
return result;
}
if (s == 4) {
if (accumulate(nums.begin(), nums.end(), 0) == target) {
result.push_back(nums);
}
return result;
}

sort(nums.begin(), nums.end());

int first = 0, second = 0, third = second + 1, fourth = s - 1;
for (first = 0; first < s - 3; first ++) {
for (second = first + 1; second < s - 2; second ++) {
third = second + 1;
fourth = s - 1;
while (third < fourth) {
int tmpSum = nums[first] + nums[second] + nums[third] + nums[fourth];
if (tmpSum == target) {
vector<int> tmp;
tmp.push_back(nums[first]);
tmp.push_back(nums[second]);
tmp.push_back(nums[third]);
tmp.push_back(nums[fourth]);
result.push_back(tmp);

do {
third ++;
} while (third < fourth && nums[third] == nums[third - 1]);
do {
fourth --;
} while (third < fourth && nums[fourth] == nums[fourth + 1]);

} else if (tmpSum < target) {
third ++;
} else {
fourth --;
}
}

while (second < s - 2 && nums[second] == nums[second + 1]) {
second ++;
}
}

while (first < s - 3 && nums[first] == nums[first + 1]) {
first ++;
}
}

return result;
}
};