![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d45adde7ea0be72f84e4df954486c318.png)
解答思路如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/148db6e5f871432015c2f840ecc2c959.png)
代码如下:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ans;
if(nums.size() < 3)
return vector<vector<int>>();
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i ++)
{
if(nums[i] > 0) break;
if(i >= 1 && nums[i] == nums[i-1]) continue;
int j = i + 1, k = nums.size() - 1, target = - nums[i];
while(j < k)
{
if (nums[j] + nums[k] < target) j++;
else if (nums[j] + nums[k] > target) k--;
else
{
ans.push_back({nums[i],nums[j],nums[k]});
k --;
j ++;
while(k > j && nums[k] == nums[k + 1]) k--;
while(k > j && nums[j] == nums[j - 1]) j++;
}
}
}
return ans;
}
};