vector<vector<int>> threeSum(vector<int>& nums) 这句代码什么意思
时间: 2023-12-03 19:04:02 浏览: 85
nums) {
vector<vector<int>> result;
if(nums.size() < 3) return result;
sort(nums.begin(), nums.end()); // sort the array
for(int i=0; i<nums.size()-2; i++) {
if(i == 0 || (i > 0 && nums[i] != nums[i-1])) { // skip duplicate elements
int low = i+1, high = nums.size()-1, sum = 0-nums[i];
while(low < high) {
if(nums[low] + nums[high] == sum) {
result.push_back({nums[i], nums[low], nums[high]});
while(low < high && nums[low] == nums[low+1]) low++; // skip duplicate elements
while(low < high && nums[high] == nums[high-1]) high--; // skip duplicate elements
low++;
high--;
} else if(nums[low] + nums[high] < sum) {
low++;
} else {
high--;
}
}
}
}
return result;
}
阅读全文