当前位置:网站首页>【7.5】15. 三数之和
【7.5】15. 三数之和
2022-07-07 21:52:00 【howtoloveyou】
对vector快排为从小到大:sort(nums.begin(), nums.end());
对vector快排为从大到小:sort(nums.begin(), nums.end(), greater<int>());
#define MAX_INF 1e7
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ret;
vector<int> ans;
vector<int> pre_ans;
int a = -MAX_INF, b, c;
int b_ptr, c_ptr;
if(nums.size() < 3) return ret;
for(int i = 0; i < nums.size() - 2; i++) {
if(a != nums[i]) {
a = nums[i];
b = nums[i + 1];
b_ptr = i + 1;
c = nums[nums.size() - 1];
c_ptr = nums.size() - 1;
while(b_ptr < c_ptr) {
//printf("a: %d, b: %d, c: %d\n", a, b, c);
if((a + b + c) < 0) {
b_ptr++;
b = nums[b_ptr];
} else if((a + b + c) > 0) {
c_ptr--;
c = nums[c_ptr];
} else {
ans.push_back(a);
ans.push_back(b);
ans.push_back(c);
if(pre_ans.size() == 0 || pre_ans != ans) {
pre_ans = ans;
ret.push_back(ans);
ans.pop_back();
ans.pop_back();
ans.pop_back();
} else if(pre_ans == ans) {
ans.pop_back();
ans.pop_back();
ans.pop_back();
}
b_ptr++;
b = nums[b_ptr];
}
}}
}
return ret;
}
};
边栏推荐
- Matlab-SEIR传染病模型预测
- LDO稳压芯片-内部框图及选型参数
- 云原生数据仓库AnalyticDB MySQL版用户手册
- The 19th Zhejiang Provincial Collegiate Programming Contest 2022浙江省赛 F.EasyFix 主席树
- The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
- Sequence of entity layer, Dao layer, service layer and controller layer
- UE4_ Ue5 panoramic camera
- 云原生正在吞噬一切,开发者该如何应对?
- Vs extension tool notes
- As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
猜你喜欢

UE4_UE5结合罗技手柄(F710)使用记录
![[compilation principle] lexical analysis design and Implementation](/img/8c/a3a50e6b029c49caf0d791f7d4513a.png)
[compilation principle] lexical analysis design and Implementation

S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption

In the field of software engineering, we have been doing scientific research for ten years!

【编译原理】词法分析设计实现

Unity3D学习笔记5——创建子Mesh

三问TDM

B_QuRT_User_Guide(36)

Technology at home and abroad people "see" the future of audio and video technology

Extended tree (I) - graphic analysis and C language implementation
随机推荐
Oracle-数据库的备份与恢复
Deep understanding of MySQL lock and transaction isolation level
FPGA basics catalog
Explain
1. Sum of two numbers
Dynamics 365 find field filtering
Happy gathering time
树后台数据存储(採用webmethod)[通俗易懂]
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
Conversion between commonsmultipartfile and file
Bea-3xxxxx error code
系统架构设计师备考经验分享:论文出题方向
Matlab SEIR infectious disease model prediction
UE4_UE5全景相机
System design overview
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
LeeCode -- 6. Zigzag transformation
V-for traversal object
力扣解法汇总648-单词替换
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行