一、题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5ca85b24fbb7e1a52c26dcbcc5e5dace.png)
二、代码
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int size_of_nums=nums.size();
vector<vector<int>> return_vector;
vector<int> return_vector_small;
int i,j;
int left;
int right;
sort(nums.begin(),nums.end());
for(i=0;i<nums.size();i++)
{
std::cout<<" "<< nums[i]<<" ";
}
std::cout<<endl;
for(i=0;i<size_of_nums;i++)
{
if(nums[i]>0) break;
if(i>=1&&nums[i]==nums[i-1]) continue;
left=i+1;
right=size_of_nums-1;
while(left<right)
{
if(nums[i]+nums[left]+nums[right]<0)
{
left=left+1;
if(left>=right) break;
}
if(nums[i]+nums[left]+nums[right]>0)
{
right=right-1;
if(left>=right) break;
}
if(nums[i]+nums[left]+nums[right]==0)
{
std::cout<<" i "<<i<<std::endl;
std::cout<<" left "<<left<<std::endl;
std::cout<<" right "<<right<<std::endl;
return_vector_small.clear();
return_vector_small.push_back(nums[i]);
return_vector_small.push_back(nums[left]);
return_vector_small.push_back(nums[right]);
return_vector.push_back(return_vector_small);
while(nums[left]==nums[left+1]&&left<right-1) left=left+1;
left=left+1;
if(left>=right) break;
}
}
}
return return_vector;
}
};
三、运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/64d9402f46f09b7e9eebc77025804416.png)