0015. 三数之和 #201
0015. 三数之和
#201
Replies: 1 comment
-
参考c++代码: class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
vector<vector<int>> res;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i = 0; i < n - 2; i++){
if(i && nums[i] == nums[i - 1]) continue;
int j = i + 1, k = n - 1;
while(j < k){
int s = nums[i] + nums[j] + nums[k];
if(s > 0) k--;
else if(s < 0) j++;
else{
res.push_back({nums[i], nums[j], nums[k]});
for (++j; j < k && nums[j] == nums[j - 1]; ++j);
for (--k; k > j && nums[k] == nums[k + 1]; --k);
}
}
}
return res;
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
0015. 三数之和
https://algo.itcharge.cn/Solutions/0001-0099/3sum/
Beta Was this translation helpful? Give feedback.
All reactions