当前位置:网站首页>【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;
}
};
边栏推荐
- One week learning summary of STL Standard Template Library
- Talk about the design and implementation logic of payment process
- Matlab 信号处理【问答随笔·2】
- Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
- 经纬度PLT文件格式说明
- 树后台数据存储(採用webmethod)[通俗易懂]
- The efficient s2b2c e-commerce system helps electronic material enterprises improve their adaptability in this way
- Mysql索引优化实战二
- Spark 离线开发框架设计与实现
- Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
猜你喜欢
随机推荐
STL标准模板库(Standard Template Library)一周学习总结
USB (XV) 2022-04-14
系统架构设计师备考经验分享:论文出题方向
UE4_UE5结合罗技手柄(F710)使用记录
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
B_QuRT_User_Guide(37)
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
Oracle-数据库的备份与恢复
UE4_ Ue5 panoramic camera
Freelink open source call center design idea
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
Markdown
php 使用阿里云存储
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
Matlab 信号处理【问答随笔·2】
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
云原生数据仓库AnalyticDB MySQL版用户手册
Live-Server使用
Matlab-SEIR传染病模型预测
Coreseek: the second step is index building and testing









