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)
思路:固定一个,其余移动。
http://blog.csdn.net/zhouworld16/article/details/16917071 分析很好,我第一个用set去重,超时。 我的代码移动的地方比人家麻烦。
可以排序,其实还可用hash。
http://blog.csdn.net/nanjunxiao/article/details/12524405 分析也很好。
正确代码:
#include <algorithm>
class Solution {
public:
void InsertSort(vector<int> &a, int n)
{
int temp;
for (int i = 1; i < n; ++i)
{
temp = a[i];
int j;
for (j = i; j > 0 && temp < a[j - 1]; --j)
{
a[j] = a[j - 1];
}
a[j] = temp;
}
}
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.size() < 3) {
return vector<vector<int>>();
}
// sort(nums.begin(), nums.end());
InsertSort(nums, nums.size());
vector<vector<int> > result;
for (int i = 0; i < nums.size() - 2 && nums[i] <= 0; ++i) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int b = i + 1;
int e = nums.size() - 1;
while(b < nums.size() - 1 && e > b) {
if (nums[i] + nums[b] + nums[e] == 0) {
vector<int> a;
a.push_back(nums[i]);
a.push_back(nums[b]);
a.push_back(nums[e]);
result.push_back(a);
while (++b < e && nums[b] == nums[b - 1]) {
}
while (b < --e && nums[e] == nums[e + 1]) {
}
continue;
}
if (nums[i] + nums[b] + nums[e] > 0) {
if (nums[i] + nums[b] + nums[e - 1] >= 0) {
--e;
continue;
} else if (nums[i] + nums[b] + nums[e - 1] < 0) {
++b;
continue;
}
}
if (nums[i] + nums[b] + nums[e] < 0) {
if (nums[i] + nums[b + 1] + nums[e] <= 0) {
++b;
continue;
} else if (nums[i] + nums[b + 1] + nums[e] > 0) {
--e;
continue;
}
}
}
}
return result;
}
};
-----------
超时代码:
#include <algorithm>
class Solution {
public:
void InsertSort(vector<int> &a, int n)
{
int temp;
for (int i = 1; i < n; ++i)
{
temp = a[i];
int j;
for (j = i; j > 0 && temp < a[j - 1]; --j)
{
a[j] = a[j - 1];
}
a[j] = temp;
}
}
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.size() < 3) {
return vector<vector<int>>();
}
// sort(nums.begin(), nums.end());
InsertSort(nums, nums.size());
vector<vector<int> > result;
for (int i = 0; i < nums.size() - 2 && nums[i] <= 0; ++i) {
int b = i + 1;
int e = nums.size() - 1;
while(b < nums.size() - 1 && e > b) {
if (nums[i] + nums[b] + nums[e] == 0) {
vector<int> a;
a.push_back(nums[i]);
a.push_back(nums[b]);
a.push_back(nums[e]);
result.(a);
++b;
--e;
continue;
}
if (nums[i] + nums[b] + nums[e] > 0) {
if (nums[i] + nums[b] + nums[e - 1] >= 0) {
--e;
continue;
} else if (nums[i] + nums[b] + nums[e - 1] < 0) {
++b;
continue;
}
}
if (nums[i] + nums[b] + nums[e] < 0) {
if (nums[i] + nums[b + 1] + nums[e] <= 0) {
++b;
continue;
} else if (nums[i] + nums[b + 1] + nums[e] > 0) {
--e;
continue;
}
}
}
}
vector<vector<int> > a(result.begin(), result.end());
return a;
}
};
------------------
第一个链接代码:
//LeetCode_3Sum
//Written by zhou
//2013.11.23
class Solution {
public:
//插入排序
void InsertSort(vector<int> &a, int n)
{
int temp;
for (int i = 1; i < n; ++i)
{
temp = a[i];
int j;
for (j = i; j > 0 && temp < a[j - 1]; --j)
{
a[j] = a[j - 1];
}
a[j] = temp;
}
}
vector<vector<int> > threeSum(vector<int> &num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int>> res;
if (num.size() < 3) //小于3个数
return res;
//对原数组非递减(递增)排序
InsertSort(num,num.size());
for (int i = 0; i < num.size(); ++i)
{
//去重
if (i != 0 && num[i] == num[i-1])
continue;
int p = i + 1, q = num.size() - 1;
int sum = 0;
//收缩法寻找第2,第3个数
while (p < q)
{
sum = num[i] + num[p] + num[q];
if (sum == 0)
{
vector<int> newRes;
newRes.push_back(num[i]);
newRes.push_back(num[p]);
newRes.push_back(num[q]);
InsertSort(newRes,newRes.size());
res.push_back(newRes);
//寻找其他可能的2个数,顺带去重
while (++p < q && num[p-1] == num[p])
{
//do nothing
}
while (--q > p && num[q+1] == num[q])
{
//do noghing
}
}
else if (sum < 0) //和太小,p向后移动
{
++p;
}
else //和过大,q向前移动
{
--q;
}
}
}
return res;
}
};