标签:ack back push public sort lse size i++ end
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n=nums.size();
vector<vector<int>>ans;
if(n<3) return{};
sort(nums.begin(),nums.end());
for(int i=0;i<n;i++){
if(nums[i]>0) break;
if(i>0 && nums[i]==nums[i-1]) continue;
int l=i+1,r=n-1;
while(l<r){
if(nums[i]+nums[l]+nums[r]==0){
ans.push_back({nums[i],nums[l],nums[r]});
while(l<r && nums[l]==nums[l+1]) l++;
while(l<r && nums[r]==nums[r-1]) r--;
l++,r--;
}else if(nums[i]+nums[l]+nums[r]>0) r--;
else l++;
}
}
return ans;
}
};
标签:ack back push public sort lse size i++ end
原文地址:https://www.cnblogs.com/rign-lr7rq2truq/p/14382287.html