【题目描述】Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
【解题思路】我们对原数组进行排序,然后开始遍历排序后的数组,这里注意不是遍历到最后一个停止,而是到倒数第三个就可以了。这里我们可以先做个剪枝优化,就是当遍历到正数的时候就break,为啥呢,因为我们的数组现在是有序的了,如果第一个要fix的数就是正数了,那么后面的数字就都是正数,就永远不会出现和为0的情况了。然后我们还要加上重复就跳过的处理,处理方法是从第二个数开始,如果和前面的数字相等,就跳过,因为我们不想把相同的数字fix两次。对于遍历到的数,用0减去这个fix的数得到一个target,然后只需要再之后找到两个数之和等于target即可。我们用两个指针分别指向fix数字之后开始的数组首尾两个数,如果两个数和正好为target,则将这两个数和fix的数一起存入结果中。然后就是跳过重复数字的步骤了,两个指针都需要检测重复数字。如果两数之和小于target,则我们将左边那个指针i右移一位,使得指向的数字增大一些。同理,如果两数之和大于target,则我们将右边那个指针j左移一位,使得指向的数字减小一些。
【考查内容】查找
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
if (nums.empty() || nums.back() < 0 || nums.front() > 0) return {};
for (int k = 0; k < nums.size(); ++k) {
if (nums[k] > 0) break;
if (k > 0 && nums[k] == nums[k - 1]) continue;
int target = 0 - nums[k];
int i = k + 1, j = nums.size() - 1;
while (i < j) {
if (nums[i] + nums[j] == target) {
res.push_back({nums[k], nums[i], nums[j]});
while (i < j && nums[i] == nums[i + 1]) ++i;
while (i < j && nums[j] == nums[j - 1]) --j;
++i; --j;
} else if (nums[i] + nums[j] < target) ++i;
else --j;
}
}
return res;
}
};
【题目描述】Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution {
public:
int threeSumClosest(vector &num, int target) {
int n = num.size();
int result = num[0] + num[1] + num[n-1];
sort(num.begin(),num.end());
for(int i=0;i<n-2;i++)
{
int start = i + 1;
int end = n - 1;
while(start < end)
{
int sum = num[i] + num[start] + num[end];
if(sum < target)
start++;
else
end–;
if(abs(sum-target) < abs(result-target))
result = sum;
}
}
return result;
}
};
【题目描述】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:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
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)
【解题思路】类似3sum求解。用两个for循环,选取第一个和第二个,然后再用前后指针求解第三个和第四个数据。
【考查内容】查找
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<int> sum;
if(num.size() < 4)
return res;
sort(num.begin(),num.end());
for(int i = 0;i < num.size()-3;i++){
sum.push_back(num[i]);
for(int j = i+1;j < num.size()-2;j++){
sum.push_back(num[j]);
int begin = j+1;
int end = num.size()-1;
while(begin < end){
if(num[i]+num[j]+num[begin]+num[end] == target){
sum.push_back(num[begin]);
sum.push_back(num[end]);
res.push_back(sum);
sum.pop_back();
sum.pop_back();
begin++;
while(begin < end && num[begin] == num[begin-1])
begin++;
end--;
}else if(num[i]+num[j]+num[begin]+num[end] < target)
begin++;
else
end--;
}
sum.pop_back();
while(j < num.size()-2 && num[j] == num[j+1])
j++;
}
sum.pop_back();
while(i < num.size()-3 && num[i] == num[i+1])
i++;
}
return res;
}
private:
vector<vector<int> > res;
};