当前位置:网站首页>【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;
}
};
边栏推荐
- UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)
- USB (XIV) 2022-04-12
- FreeLink开源呼叫中心设计思想
- Install a new version of idea. Double click it to open it
- 漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
- PCI-Express接口的PCB布线规则
- Inftnews | web5 vs Web3: the future is a process, not a destination
- Archlinux install MySQL
- B / Qurt Utilisateur Guide (36)
- List. How to achieve ascending and descending sort() 2020.8.6
猜你喜欢

USB (XV) 2022-04-14

Mysql索引优化实战一

城联优品作为新力量初注入,相关上市公司股价应声上涨150%

UE4_UE5结合罗技手柄(F710)使用记录

Deep understanding of MySQL lock and transaction isolation level

ROS2专题(03):ROS1和ROS2的区别【02】

Unity3d Learning Notes 6 - GPU instantiation (1)

Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry

USB(十五)2022-04-14

Oracle database backup and recovery
随机推荐
System design overview
leetcode-520. Detect capital letters -js
漏洞复现----49、Apache Airflow 身份验证绕过 (CVE-2020-17526)
B_ QuRT_ User_ Guide(37)
B_QuRT_User_Guide(37)
The 19th Zhejiang Provincial College Programming Contest 2022 f.easyfix chairman tree
UE4_UE5全景相机
Technology at home and abroad people "see" the future of audio and video technology
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
v-for遍历对象
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
生鲜行业数字化采购管理系统:助力生鲜企业解决采购难题,全程线上化采购执行
Dynamic agent explanation (July 16, 2020)
Unity3D学习笔记5——创建子Mesh
Mysql索引优化实战一
Freelink open source call center design idea
LDO穩壓芯片-內部框圖及選型參數
windows设置redis开启自动启动
Live-Server使用
Unity3d Learning Notes 6 - GPU instantiation (1)