class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> ret;
sort(nums.begin(),nums.end());
int i=0;
while(i<nums.size())
{
while(i>0&&i<nums.size()&&nums[i]==nums[i-1])
{
i++;
}
int left = i+1;
int right = nums.size()-1;
while(left<right)
{
int sum = nums[i]+nums[left]+nums[right];
if(sum<0)
{
left++;
}
else if(sum>0)
{
right--;
}
else
{
ret.push_back({nums[i],nums[left],nums[right]});
left++;
right--;
while(left<right&&nums[left]==nums[left-1])
{
left++;
}
while(left<right&&nums[right]==nums[right+1])
{
right--;
}
}
}
i++;
}
return ret;
}
};