当前位置:网站首页>【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 Utilisateur Guide (36)
- 云原生正在吞噬一切,开发者该如何应对?
- B_QuRT_User_Guide(36)
- How to login and enable synchronization function in Google browser
- FreeLink开源呼叫中心设计思想
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- re1攻防世界逆向
- Conversion between commonsmultipartfile and file
- 给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
- USB (XV) 2022-04-14
猜你喜欢
B_ QuRT_ User_ Guide(37)
S2b2b mall solution of intelligent supply chain in packaging industry: opening up a new ecosystem of e-commerce consumption
ROS2专题(03):ROS1和ROS2的区别【02】
Open source hardware small project: anxinco esp-c3f control ws2812
B_ QuRT_ User_ Guide(36)
USB (XV) 2022-04-14
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
MATLAB signal processing [Q & A essays · 2]
B / Qurt Utilisateur Guide (36)
JS get the key and value of the object
随机推荐
Happy gathering time
Illegal behavior analysis 1
Explain
In the field of software engineering, we have been doing scientific research for ten years!
USB (XVIII) 2022-04-17
Dynamic agent explanation (July 16, 2020)
StringUtils工具类
经纬度PLT文件格式说明
One week learning summary of STL Standard Template Library
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
Design and implementation of spark offline development framework
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
[untitled]
Explain
Conversion between commonsmultipartfile and file
FPGA basics catalog
SAP 内存参数调优过程
Installing spss25
Cloud native data warehouse analyticdb MySQL user manual