当前位置:网站首页>【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;
}
};
边栏推荐
- Windows set redis to start automatically
- 在软件工程领域,搞科研的这十年!
- Tree background data storage (using webmethod) [easy to understand]
- Dynamics 365 find field filtering
- 电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
- PCI-Express接口的PCB布线规则
- ROS2专题(03):ROS1和ROS2的区别【02】
- Matlab 信号处理【问答随笔·2】
- Three questions TDM
- How to login and enable synchronization function in Google browser
猜你喜欢
Oracle database backup and recovery
B_ QuRT_ User_ Guide(37)
UE4_UE5结合罗技手柄(F710)使用记录
Mysql索引优化实战二
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
Mobile heterogeneous computing technology - GPU OpenCL programming (basic)
Extended tree (I) - graphic analysis and C language implementation
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Cloud native is devouring everything. How should developers deal with it?
SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
随机推荐
B_QuRT_User_Guide(37)
php 使用阿里云存储
Add data analysis tools in Excel
B_QuRT_User_Guide(36)
Cloud native is devouring everything. How should developers deal with it?
Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
First week of July
Force deduction solution summary 648 word replacement
LeeCode -- 6. Zigzag transformation
B_ QuRT_ User_ Guide(40)
B_QuRT_User_Guide(40)
POJ2392 SpaceElevator [DP]
windows设置redis开启自动启动
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
Illegal behavior analysis 1
谷歌浏览器怎么登录及开启同步功能
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
B_ QuRT_ User_ Guide(36)
LDO穩壓芯片-內部框圖及選型參數
Caip2021 preliminary VP