当前位置:网站首页>Nc54 sum of three numbers
Nc54 sum of three numbers
2022-07-22 20:12:00 【henujolly】
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int>>ans;
sort(num.begin(),num.end());
for(int i=0;i<num.size();i++){
if(num[i]>0) break;
if(i>0&&num[i]==num[i-1]) continue;
int target=0-num[i];
int l=i+1,r=num.size()-1;
while(l<r){
if(num[l]+num[r]==target){
while(num[l]==num[l+1]&&l<r) l++;
while(num[r]==num[r-1]&&l<r) r--;
ans.push_back({
num[i],num[l],num[r]});
l++;
r--;
}
else if(num[l]+num[r]<target) l++;
else r--;
}
}
return ans;
}
};
边栏推荐
猜你喜欢
随机推荐
When the appsstore transporter uploads, it is stuck in the process of verifying the problem
Xcode11 添加lanuchimage黑屏无法显示问题
【C】从内存出发理解C语言变量作用域与生命周期
Spark SQL built-in functions and custom functions UDF
appsstore transporter上传的时候一直卡在正在验证问题
2017年终总结
ES6用法
HDU5667 Sequence
Flutter 2进阶(九):FijkPlayer播放视频与卡片效果
为什么memset不能将数组元素初始化为1?
Xilinx FPGA软核开发流程
Elastic Search 学习入门之核心概念(四)
euler五十讲(一)
oc 项目倒入swift 代码注意事项
Kotlin学习二:类与对象
Development and construction of NFT card chain game system DAPP
xcode 编译pod 第三方库报错
var、let、const区别
Kotlin learning 1: variables, functions, conditional statements and loop statements
关闭浏览器 如何清除localStorage数据