vector<vector<int>> threeSum(vector<int>& nums)什么意思
时间: 2023-12-03 17:04:01 浏览: 88
nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end()); // sort the array in ascending order
int n = nums.size();
for(int i=0; i<n-2; i++) {
if(i==0 || (i>0 && nums[i]!=nums[i-1])) { // skip duplicates
int lo = i+1, hi = n-1, sum = 0-nums[i];
while(lo<hi) {
if(nums[lo]+nums[hi]==sum) {
res.push_back({nums[i], nums[lo], nums[hi]});
while(lo<hi && nums[lo]==nums[lo+1]) lo++; // skip duplicates
while(lo<hi && nums[hi]==nums[hi-1]) hi--; // skip duplicates
lo++; hi--;
} else if(nums[lo]+nums[hi]<sum) lo++;
else hi--;
}
}
}
return res;
}
阅读全文