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)
分析:分治法考虑,对于每一个A[i],将3sum转化为two sum解法,即去掉A[i]后target = 0 - A[i]。实现时需要用到多个hash表,同时去重也不太好解决,需要重新思考一下方案。另外一个方案时参考wiki的算法,也是转化为2sum,不过更为巧妙。
sort(S);
for i=0 to n-3 do
a = S[i];
k = i+1;
l = n-1;
while (k<l) do
b = S[k];
c = S[l];
if (a+b+c == 0) then
output a, b, c;
//Following will help printing all 3 combinations that sum to zero.
k = k + 1
l = l - 1
else if (a+b+c > 0) then
l = l - 1;
else
k = k + 1;
end
end
end
注意要去重,下面的实现就简单了:
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > ret;
sort(num.begin(), num.end());
for(int i = 0; i < num.size(); i++) {
if(i != 0 && num[i] == num[i - 1]) continue;
int j = i + 1;
int k = num.size() - 1;
while(j < k) {
int sum = num[i] + num[j] + num[k];
if(sum > 0) {
k--;
}
else if(sum < 0) {
j++;
}
else if(sum == 0) {
if(j != i + 1 && num[j] == num[j - 1]) {
j++;
}
else if(k != num.size() - 1 && num[k] == num[k+1]) {
k--;
}
else {
vector<int> tmp(3);
tmp[0] = num[i];
tmp[1] = num[j];
tmp[2] = num[k];
ret.push_back(tmp);
j++;
k--;
}
}
}
}
return ret;
}
};
=====================================================================================
4Sum
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,时间复杂度O(n^3)
class Solution {
public:
vector<vector<int> > fourSum(vector<int> &num, int target) {
vector<vector<int> > ret;
sort(num.begin(), num.end());
int len = num.size();
for(int i = 0; i <= len - 4; i++) {
if(i != 0 && num[i] == num[i - 1]) continue;
for(int j = i + 1; j <= len - 3; j++) {
if(j != i + 1 && num[j] == num[j - 1]) continue;
int k = j + 1;
int l = len - 1;
while(k < l) {
int sum = num[i] + num[j] + num[k] + num[l];
if(k != j+1 && num[k] == num[k-1]) {
k++;
} else if(l != len - 1 && num[l] == num[l+1]) {
l--;
} else {
if(sum > target) {
l--;
} else if(sum < target) {
k++;
} else {
vector<int> tmp(4);
tmp[0] = num[i];
tmp[1] = num[j];
tmp[2] = num[k];
tmp[3] = num[l];
ret.push_back(tmp);
k++;
l--;
}
}
}
}
}
return ret;
}
};
=====================================================================================
3Sum Closest
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).
分析:可以采用3Sum相同思路,不过在target判断时会有所不同
class Solution {
public:
int threeSumClosest(vector<int> &num, int target) {
int closet;
bool initCloset = true;
sort(num.begin(), num.end());
for(int i = 0; i < num.size(); i++) {
if(i != 0 && num[i] == num[i - 1]) continue;
int j = i + 1;
int k = num.size() - 1;
while(j < k) {
int sum = num[i] + num[j] + num[k];
if(j != i + 1 && num[j] == num[j - 1]) {
j++;
} else if(k != num.size() - 1 && num[k] == num[k+1]) {
k--;
} else {
//处理sum,target。 使用closet来记录上次目标值
if(initCloset || abs(sum - target) < abs(closet - target)) {
closet = sum;
initCloset = false;
}
if(sum > target) {
k--;
} else if(sum < target) {
j++;
} else if(sum == target) {
return sum;
}
}
}
}
return closet;
}
};
这里注意closet需要初始化,下面的结构来处理 “初始化” 和 “已初始化” 状态比较好
bool init = true;
int ret;
if(init || xxxx) {
xxxx
init = false;
}