当前位置:网站首页>【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;
}
};
边栏推荐
- 进度播报|广州地铁七号线全线29台盾构机全部完成始发
- CXF call reports an error. Could not find conduct initiator for address:
- js 获取对象的key和value
- SRM supplier cloud collaborative management platform solution for building materials industry to realize business application scalability and configuration
- POJ2392 SpaceElevator [DP]
- B_ QuRT_ User_ Guide(36)
- USB (XVI) 2022-04-28
- Talk about the design and implementation logic of payment process
- LDO穩壓芯片-內部框圖及選型參數
- Matlab 信号处理【问答随笔·2】
猜你喜欢

js 获取对象的key和value

USB(十五)2022-04-14

Matlab 信号处理【问答随笔·2】

Digital procurement management system for fresh food industry: help fresh food enterprises solve procurement problems and implement online procurement throughout the process

Markdown

B_ QuRT_ User_ Guide(37)

PCI-Express接口的PCB布线规则

MySQL Index Optimization Practice I

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

Extended tree (I) - graphic analysis and C language implementation
随机推荐
B_QuRT_User_Guide(37)
V-for traversal object
Unity3D学习笔记5——创建子Mesh
js 获取对象的key和value
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
三问TDM
MySQL Index Optimization Practice II
Conversion between commonsmultipartfile and file
In the field of software engineering, we have been doing scientific research for ten years!
Oracle database backup and recovery
Cloud native data warehouse analyticdb MySQL user manual
Unity3d learning notes 5 - create sub mesh
Explain
PCB wiring rules of PCI Express interface
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
2022 届的应届生都找到工作了吗?做自媒体可以吗?
USB (XVII) 2022-04-15