当前位置:网站首页>【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;
}
};
边栏推荐
- B_QuRT_User_Guide(39)
- Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
- Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
- C inheritance and interface design polymorphism
- Unity3d learning notes 4 - create mesh advanced interface
- SAP HR 劳动合同信息 0016
- Installing spss25
- 系统设计概述
- LeeCode -- 6. Z 字形变换
- 深入理解Mysql锁与事务隔离级别
猜你喜欢
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
[stm32+esp8266 connect Tencent cloud IOT development platform 2] stm32+esp8266-01s connect Tencent cloud
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
Flash encryption process and implementation of esp32
Lm12 rolling heikin Ashi double K-line filter
在软件工程领域,搞科研的这十年!
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
Markdown
B_ QuRT_ User_ Guide(36)
Ros2 topic (03): the difference between ros1 and ros2 [02]
随机推荐
伸展树(一) - 图文解析与C语言实现
深入理解Mysql锁与事务隔离级别
Live-Server使用
FPGA basics catalog
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的
Installing spss25
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
PCB wiring rules of PCI Express interface
三问TDM
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
B_ QuRT_ User_ Guide(40)
2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
Count the top 10 films at the box office and save them in another file
MySQL Index Optimization Practice I
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
2022 届的应届生都找到工作了吗?做自媒体可以吗?
SAP HR 社会工作经历 0023
HDU 4747 Mex「建议收藏」
Unity3d Learning Notes 6 - GPU instantiation (1)