当前位置:网站首页>【7.5】15. 三数之和
【7.5】15. 三数之和
2022-07-07 21:52:00 【howtoloveyou】
对vector快排为从小到大:sort(nums.begin(), nums.end());
对vector快排为从大到小: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;
}
};
边栏推荐
- Dynamic agent explanation (July 16, 2020)
- 力扣解法汇总648-单词替换
- B_QuRT_User_Guide(38)
- New potential energy of industrial integration, Xiamen station of city chain technology digital summit successfully held
- USB (XV) 2022-04-14
- Cloud native is devouring everything. How should developers deal with it?
- V-for traversal object
- 648. Word replacement
- List. How to achieve ascending and descending sort() 2020.8.6
- UE4_ Ue5 panoramic camera
猜你喜欢
STL标准模板库(Standard Template Library)一周学习总结
Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process
Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
leetcode-520. Detect capital letters -js
[compilation principle] lexical analysis design and Implementation
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Lm12 rolling heikin Ashi double K-line filter
LDO穩壓芯片-內部框圖及選型參數
LeeCode -- 6. Z 字形变换
MATLAB signal processing [Q & A essays · 2]
随机推荐
CXF call reports an error. Could not find conduct initiator for address:
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
2022 Season 6 perfect children's model Shaanxi finals came to a successful conclusion
2022第六季完美童模陕西总决赛圆满落幕
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
Unity3d Learning Notes 6 - GPU instantiation (1)
Explain
Freelink open source call center design idea
B_QuRT_User_Guide(37)
HDU 4747 Mex「建议收藏」
V-for traversal object
Installing spss25
JS get the key and value of the object
Unity3D学习笔记6——GPU实例化(1)
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."
First week of July
Ros2 topic (03): the difference between ros1 and ros2 [01]
Mysql索引优化实战一