当前位置:网站首页>【7.5】15. Sum of three numbers
【7.5】15. Sum of three numbers
2022-07-07 23:34:00 【howtoloveyou】
Yes vector Line up quickly from small to large :sort(nums.begin(), nums.end());
Yes vector Line up quickly from big to small :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;
}
};
边栏推荐
- 伸展树(一) - 图文解析与C语言实现
- Mysql索引优化实战二
- Unity3d Learning Notes 6 - GPU instantiation (1)
- Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
- B_QuRT_User_Guide(40)
- Unity3d learning notes 5 - create sub mesh
- 经纬度PLT文件格式说明
- B_QuRT_User_Guide(36)
- The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
- Coreseek: the second step is index building and testing
猜你喜欢

Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process

移动端异构运算技术 - GPU OpenCL 编程(基础篇)

Right click the idea file to create new. There is no solution to create new servlet

LDO稳压芯片-内部框图及选型参数

2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?

SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration

2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
![Ros2 topic (03): the difference between ros1 and ros2 [01]](/img/20/39d47c93400050a7bc8ad7efea51b3.png)
Ros2 topic (03): the difference between ros1 and ros2 [01]

List. How to achieve ascending and descending sort() 2020.8.6

ROS2专题(03):ROS1和ROS2的区别【01】
随机推荐
Turbo introder common scripts
深入理解Mysql锁与事务隔离级别
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
First week of July
B_ QuRT_ User_ Guide(36)
Ros2 topic (03): the difference between ros1 and ros2 [02]
StringUtils工具类
【7.4】25. K 个一组翻转链表
USB (XIV) 2022-04-12
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
How to login and enable synchronization function in Google browser
LeeCode -- 6. Zigzag transformation
Installing spss25
Archlinux install MySQL
B_QuRT_User_Guide(36)
System design overview
2021icpc Shanghai h.life is a game Kruskal reconstruction tree