当前位置:网站首页>Niuke.com: sum of three numbers
Niuke.com: sum of three numbers
2022-06-12 20:04:00 【lsgoose】


Catalog
Method 1 : violence
The triple cycle should come to mind
But this can't pass
What shall I do? , Just use one O(nlogn) Reduce the complexity by sorting the complexity , How to reduce it ? After sorting, the same elements will be together , If we encounter the same problem, just skip it .
The code is as follows :
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int>> res;
if(num.size()<3) return res;
sort(num.begin(), num.end());
for(int i=0;i<num.size()-2;++i){
if(i&&num[i]==num[i-1]) continue;
for(int j=i+1;j<num.size()-1;++j){
if(j>i+1&&num[j]==num[j-1]) continue;
for(int k=j+1;k<num.size();++k){
if(k>j+1&&num[k]==num[k-1]) continue;
if(num[i]+num[j]+num[k]==0){
res.push_back({num[i],num[j],num[k]});
}
}
}
}
return res;
}
};Method 2 : Double pointer
We can use the double pointer method to reduce the time complexity to O(n^2), Sort first , Because this allows us to skip repeating elements . Secondly, it is convenient for us to use double pointers .
Next, the numbers in the array are processed as follows :
1. Set pointers to the next two numbers , That is, the left and right pointers of the rear area
- If three numbers add up, they are equal to 0, Put three numbers in the answer , Then update the two pointers to the non repeating element position
- If it is greater than 0, Move the right pointer one bit to the left
- If it is less than 0, Move the left pointer one bit to the right
The exit condition is the position of the left pointer >= The position of the right pointer
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int>> res;
if(num.size()<3) return res;
sort(num.begin(), num.end());
for(int i=0;i<num.size();++i){
if(i&&num[i]==num[i-1]) continue;
int l=i+1,r=num.size()-1;
while(l<r){
if(num[i]+num[l]+num[r]==0){
res.push_back({num[i],num[l],num[r]});
while(l+1<r&&num[l]==num[l+1]) ++l;
while(r-1>l&&num[r]==num[r-1]) --r;
++l,--r;
}else if(num[l]+num[r]+num[i]>0){
--r;
}else{
++l;
}
}
}
return res;
}
};边栏推荐
- The execution results of i+=2 and i++ i++ under synchronized are different
- Demand and business model innovation-5-process
- Macro definitions and functions
- The joint empowerment plan of Baidu PaddlePaddle large enterprise open innovation center was launched! Help Pudong to upgrade its industry intelligently
- QT -- how to get the contents of selected cells in qtableview
- Continuously improve the overall performance of adaoracle Oracle Oracle
- Experience Technology Department of ant group launched the 2023rd school recruitment
- If you master these skills, you can also write a high score resume in the eyes of HR
- The Milvus graphical management tool Attu is coming!
- Torch network model is converted to onnx format and visualized
猜你喜欢

Reading small program based on wechat e-book graduation design (4) opening report
![[games101] class note 8 - shading (shading frequency, graphics pipeline, texture mapping)](/img/90/f23c3fd9386521ec9254943bff2a0d.png)
[games101] class note 8 - shading (shading frequency, graphics pipeline, texture mapping)

解释器文件

在 Traefik Proxy 2.5 中使用/开发私有插件(Traefik 官方博客)

Demand and business model innovation-4-strategy

Wechat e-book reading applet graduation design works (1) development outline

How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"

Demand and business model innovation - demand 4- overview of demand acquisition

开源深度学习框架PlaidML安装及测试

Since using low code development, the development efficiency has been increased by 10 times
随机推荐
登录mysql
Ctfshow-web266 (deserialization)
EASYCODE one click plug-in custom template
Alipay payment Episode 12: Crazy God and Feige Alipay payment configuration code (free resources, no thanks for taking them away)
Blue Bridge Cup basic-15 VIP question string comparison
Interpreter Files
进程会计、进程时间、守护进程
Operating instructions for installing mysql5.7 in centos7
User and group permissions
What does SQL replace or
sklearn中随机森林RandomForestClassifier的参数含义
Wechat jsapi payment pit summary
Process accounting, process time, daemon
新来的同事问我 where 1=1 是什么意思???
基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
标量、向量、数组和矩阵
Compilation of programs
When will the index fail
Simple understanding of anti shake function
WordPress station group tutorial automatic collection of pseudo original release tutorial