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: 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]
]
首先是自己写的代码,顺序枚举加二分查找,naive算法,复杂度O(n^2*log n),307/313 Accepted,剩下六个Case超时了,意料之中
class Solution {
public:
int array1[3] = {}, array2[3] = {};
bool cmp(vector<int> a,vector<int> b)
{
for(int i=0;i<3;i++)
{
array1[i] = a[i];
array2[i] = b[i];
}
sort(array1,array1+3);
sort(array2,array2+3);
for(int i=0;i<3;i++)
{
if(array1[i] != array2[i])
{
return false;
}
}
return true;
}
vector<vector<int> > threeSum(vector<int>& nums)
{
vector<vector<int> > res;
vector<int> tmp;
int len = nums.size();
sort(nums.begin(),nums.end());
for(int i=0;i<len-1;i++)
{
for(int j=i+1;j<len;j++)
{
int t = nums[i]+nums[j];
int k = 0-t;
int left = j+1, right = len-1, des = -1;
while(left <= right)
{
int mid = (left+right)/2;
if(nums[mid] == k)
{
des = mid; break;
}
else if(nums[mid] < k)//在右边
{
left = mid+1;
}
else
{
right = mid;
}
if(left == right)
{
if(nums[left] == k)
{
des = left;
}
break;
}
}
if(des != -1)
{
tmp.push_back(nums[i]);
tmp.push_back(nums[j]);
tmp.push_back(nums[des]);
int flag = 0;
for(int kk=0;kk<res.size();kk++)//判断是否有相同的三元组
{
if(cmp(res[kk],tmp) == true)
{
flag = 1;
}
}
if(flag == 0)
{
res.push_back(tmp);
}
tmp.clear();
}
}
}
return res;
}
};
然后是O(N^2)的算法.看了Solution里面解法之后自己写的.对于前面的nums[i],在后面的数字中找到left和right使得nums[i]+nums[left]+nums[right] == 0.那么这里可以使用两个指针移动,一共是O(N).由于答案不允许出现重复的三元组,所以要过滤掉相同的元素,过滤方法如下面代码所示.
class Solution {
public:
vector<vector<int> > threeSum(vector<int>& nums)
{
vector<vector<int> > res;
vector<int> tmp;
int len = nums.size();
sort(nums.begin(),nums.end());
for(int i=0;i<len-2;i++)
{
int left = i+1, right = len-1;
while(left < right)
{
int sum = nums[left]+nums[right];
if(sum+nums[i] < 0)
{
left++;
}
else if(sum+nums[i] > 0)
{
right--;
}
else if(sum+nums[i] == 0)//找到三元组
{
tmp.push_back(nums[i]);
tmp.push_back(nums[left]);
tmp.push_back(nums[right]);
res.push_back(tmp);
//去除重复元素
while(left+1<len && nums[left]==tmp[1])
{
left++;
}
while(right>=1 && nums[right-1]==tmp[2])
{
right--;
}
tmp.clear();
}
}
while(nums[i] == nums[i+1])
{
i++;
}
}
return res;
}
};