当前位置:网站首页>【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;
}
};
边栏推荐
- Fibonacci number of dynamic programming
- StringUtils工具类
- RE1 attack and defense world reverse
- 产业共融新势能,城链科技数字峰会厦门站成功举办
- LDO voltage stabilizing chip - internal block diagram and selection parameters
- leetcode-520. Detect capital letters -js
- LDO穩壓芯片-內部框圖及選型參數
- SLAM面试总结
- Lm12 rolling heikin Ashi double K-line filter
- Have all the fresh students of 2022 found jobs? Is it OK to be we media?
猜你喜欢

B_QuRT_User_Guide(36)

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

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

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

Understand TCP's three handshakes and four waves with love

Three questions TDM

Spark 离线开发框架设计与实现

进度播报|广州地铁七号线全线29台盾构机全部完成始发
![[compilation principle] lexical analysis design and Implementation](/img/8c/a3a50e6b029c49caf0d791f7d4513a.png)
[compilation principle] lexical analysis design and Implementation

LDO稳压芯片-内部框图及选型参数
随机推荐
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
How to change the formula picture in the paper directly into the formula in word
ESP at installation esp8266 and esp32 versions
SAP 内存参数调优过程
SAP HR 社会工作经历 0023
2022 certified surveyors are still at a loss when preparing for the exam? Teach you how to take the exam hand in hand?
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
B_QuRT_User_Guide(36)
The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
First week of July
FreeLink开源呼叫中心设计思想
Three questions TDM
UE4_ Ue5 panoramic camera
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Ros2 topic (03): the difference between ros1 and ros2 [01]
B_QuRT_User_Guide(40)
Explain
SAP HR奖罚信息导出
SQL database execution problems
Flash encryption process and implementation of esp32