题解 | #三数之和#

三数之和

http://www.nowcoder.com/practice/345e2ed5f81d4017bbb8cc6055b0b711

class Solution { public: vector<vector > threeSum(vector &num) { vector<vector>ans; vectortmp; int n=num.size(); if(n<3) return ans; sort(num.begin(),num.end()); for(int i =0;i<n-2;i++){ int j=i+1,k=n-1,target=0; while(j<k){ if(-num[i]==num[j]+num[k]){ tmp.push_back(num[i]); tmp.push_back(num[j]); tmp.push_back(num[k]); for(auto i :ans){ if (tmp==i) target=1; } if(target==0) ans.push_back(tmp); tmp.clear(); target=0; j++; k--; } else if(-num[i]<num[j]+num[k]){ k--; } else{ j++; } } } return ans; } };

全部评论
自己还是慢了点
点赞 回复 分享
发布于 2022-10-24 12:53 陕西

相关推荐

迷茫的大四🐶:价格这么低都能满了?
点赞 评论 收藏
分享
09-28 22:01
已编辑
广西科技大学 IT技术支持
合适才能收到offe...:找桌面运维?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务