双指针
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>>res;
if(nums.size() == 0)
{
return res;
}
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i++)
{
if (i > 0 && nums[i] == nums[i - 1]) continue;
if(nums[i] > 0) break;
int left = i + 1, right = nums.size() - 1;
while(left < right)
{
int temp = nums[i] + nums[left] + nums[right];
if(temp == 0)
{
vector<int>t;
t.push_back(nums[i]);
t.push_back(nums[left]);
t.push_back(nums[right]);
res.push_back(t);
left++;
right--;
while( left < right && nums[left-1] == nums[left]) left++;
while( left < right && nums[right+1] == nums[right]) right--;
}
else if(temp < 0)
{
left++;
}
else
{
right--;
}
}
}
return res;
}
};