4Sum

Desicription

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.

1
2
3
4
5
6
7
8
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

Solution

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
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> res;
if(nums.size() <= 3)
return res;
std::sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i++){
int target3 = target - nums[i];
for(int j = i + 1; j < nums.size(); j++){
int target2 = target3 - nums[j];
int left = j + 1;
int right = nums.size() - 1;
while(left < right){
int sum = nums[left] + nums[right];
if(sum < target2)
left++;
else if(sum > target2)
right--;
else{
vector<int> tmp;
tmp.push_back(nums[i]);
tmp.push_back(nums[j]);
tmp.push_back(nums[left]);
tmp.push_back(nums[right]);
res.push_back(tmp);
while(left < right && nums[left] == tmp[2])
left++;
while(left < right && nums[right] == tmp[3])
right--;
}
}
while(j + 1 < nums.size() && nums[j+1] == nums[j])
j++;
}
while(i + 1 < nums.size() && nums[i+1] == nums[i])
i++;
}
return res;
}
};