当前位置:网站首页>【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;
}
};
边栏推荐
- Map operation execution process
- 城联优品作为新力量初注入,相关上市公司股价应声上涨150%
- Illegal behavior analysis 1
- SQL database execution problems
- leetcode-520. Detect capital letters -js
- B_ QuRT_ User_ Guide(40)
- Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
- POJ2392 SpaceElevator [DP]
- 8.31 Tencent interview
- How to generate unique file names
猜你喜欢
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
伸展树(一) - 图文解析与C语言实现
B_QuRT_User_Guide(36)
Flash encryption process and implementation of esp32
Anxin can internally test offline voice module vb-01 to communicate with esp-c3-12f
经纬度PLT文件格式说明
Live-Server使用
Ros2 topic (03): the difference between ros1 and ros2 [01]
2022第六季完美童模陕西总决赛圆满落幕
Lm12 rolling heikin Ashi double K-line filter
随机推荐
B / Qurt Utilisateur Guide (36)
SAP HR 家庭成员信息
windows设置redis开启自动启动
B_ QuRT_ User_ Guide(38)
Anxinco esp32-a1s development board is adapted to Baidu dueros routine to realize online voice function
LeeCode -- 6. Z 字形变换
StringUtils工具类
leetcode-520. Detect capital letters -js
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
Open source hardware small project: anxinco esp-c3f control ws2812
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
USB (XVII) 2022-04-15
Caip2021 preliminary VP
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
树后台数据存储(採用webmethod)[通俗易懂]
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
re1攻防世界逆向
LM12丨Rolling Heikin Ashi二重K线滤波器
Live-Server使用
C inheritance and interface design polymorphism