当前位置:网站首页>【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 signal processing [Q & A essays · 2]
- List. How to achieve ascending and descending sort() 2020.8.6
- 2021ICPC上海 H.Life is a Game Kruskal重构树
- Summary of SQL single table query 2020.7.27
- USB (XVI) 2022-04-28
- [microservices SCG] gateway integration Sentinel
- Lm12 rolling heikin Ashi double K-line filter
- Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
- Inftnews | the wide application of NFT technology and its existing problems
- leetcode-520. Detect capital letters -js
猜你喜欢
进度播报|广州地铁七号线全线29台盾构机全部完成始发
B_QuRT_User_Guide(37)
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
2021icpc Shanghai h.life is a game Kruskal reconstruction tree
USB(十五)2022-04-14
Unity3D学习笔记6——GPU实例化(1)
USB (XV) 2022-04-14
Mysql索引优化实战一
Installing spss25
Lm12 rolling heikin Ashi double K-line filter
随机推荐
Dynamic agent explanation (July 16, 2020)
JS get the key and value of the object
Unity3d Learning Notes 6 - GPU instantiation (1)
V-for traversal object
谷歌浏览器怎么登录及开启同步功能
System design overview
城联优品作为新力量初注入,相关上市公司股价应声上涨150%
List. How to achieve ascending and descending sort() 2020.8.6
Oracle database backup and recovery
HDU 4747 mex "recommended collection"
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
leetcode-520. Detect capital letters -js
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
php 使用阿里云存储
深入理解Mysql锁与事务隔离级别
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
Inftnews | web5 vs Web3: the future is a process, not a destination
New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
Lm12 rolling heikin Ashi double K-line filter
Explain