当前位置:网站首页>【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;
}
};
边栏推荐
- Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
- Windows set redis to start automatically
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
- 违法行为分析1
- 2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?
- B / Qurt Utilisateur Guide (36)
- 【7.5】15. 三数之和
- 包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
- System design overview
- List. How to achieve ascending and descending sort() 2020.8.6
猜你喜欢
![[compilation principle] lexical analysis design and Implementation](/img/8c/a3a50e6b029c49caf0d791f7d4513a.png)
[compilation principle] lexical analysis design and Implementation

Anxinco EC series modules are connected to the multi protocol access products of onenet Internet of things open platform

深入理解Mysql锁与事务隔离级别

Add data analysis tools in Excel

The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way

leetcode-520. Detect capital letters -js

Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle

JS get the key and value of the object
![Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
Given an array, such as [7864, 284, 347, 7732, 8498], now you need to splice the numbers in the array to return the "largest possible number."

Matlab SEIR infectious disease model prediction
随机推荐
SAP HR奖罚信息导出
Deep understanding of MySQL lock and transaction isolation level
B_ QuRT_ User_ Guide(40)
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
B_ QuRT_ User_ Guide(39)
HDU 4747 mex "recommended collection"
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
USB (XVI) 2022-04-28
Summary of SQL single table query 2020.7.27
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
Mysql索引优化实战一
Cloud native data warehouse analyticdb MySQL user manual
FreeLink开源呼叫中心设计思想
MySQL Index Optimization Practice I
B / Qurt Utilisateur Guide (36)
Ros2 topic (03): the difference between ros1 and ros2 [02]
B_QuRT_User_Guide(36)
UE4_ Ue5 panoramic camera