3sum 求三数之和等于0,不允许重复
https://leetcode.com/problems/3sum/
套路比较常见了,最重要的是去重。还是没法一次通过。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& a) {
vector<vector<int>> ans;
int n = a.size();
if(n < ) return ans;
sort(a.begin(),a.end());
for(int i = ; i < n - ; i++)
{
int target = - a[i];
for(int j = i + , k = n - ; j < k; )
{
int sum2 = a[j] + a[k];
if(sum2 == target)
{
vector<int> tmp{a[i],a[j],a[k]};
ans.push_back(tmp);
// 这一行做完以后,a[j]依然等于a[j-1]。
while(j+ < n && a[j+]==a[j]) j++;
j++;
while(k- >= && a[k-]==a[k]) k--;
k--;
}
else if(sum2 < target)
{
j++;
}
else
{
k--;
}
}
//这一步并不总是执行的,只是走到所有的重复的最后。
while(i+ < n && a[i+] == a[i]) i++;
}
return ans;
}
};
去重的原理要好好想一下:
比如 -2 -2 -2 0 1 1 1 1 2
第一次i选中-2的时候,后面的组合有0 2,1 1.
这之后,如果再选第二个-2作为第一个数的话,后面依然会产生0 2 , 1 1,就发生了重复。
因为,如果第一次在集合A中找2,第二次相当于在A的子集中找2。第二次找出的结果肯定是第一次结果的子集。
所以,第一次选-2以后,第二次选应该找到第一个不是-2的数。这样去重。
3sum 求三数之和等于0,不允许重复的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- 15. 3Sum[M]三数之和
题目 Given an array nums of n integers, are three elements a, b, c in nums such that a+b+c=0? Find all ...
- 259 [LeetCode] 3Sum Smaller 三数之和较小值
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- [LeetCode] 15. 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
随机推荐
- Kali无法启动、无法安装
好久未用Kali,换了个电脑后拷贝过去,发现打开登录界面无限循环,后删除下载新的vm镜像,发现无法开机,界面如下: 后经过各种尝试,发现为vmware版本太低的原因造成,升级vmware到版本14,可 ...
- 前端基础:canvas概述
1:并不是所有的浏览器都支持canvas,若是不支持则浏览器会弹出 <canvas>Update your browser to enjoy canvas</canvas> 浏 ...
- 思科模拟器-使用vlan划分子网
此博客为初始化版本,未经完善,后续我再整理一下 小技巧:有时候我们不小心执行了一条错误命令,可以尝试使用在该命令前加一个no进行撤销 首先说一下网络设备的选择 在该实验中我主要用到一台三层交换机(思科 ...
- Django学习笔记之视图高级-错误处理
错误处理 在一些网站开发中.经常会需要捕获一些错误,然后将这些错误返回比较优美的界面,或者是将这个错误的请求做一些日志保存.那么我们本节就来讲讲如何实现. 常见的错误码 404:服务器没有指定的url ...
- 我发起了一个 ILBC 的 子项目 ILBC Studio
ILBC 见 <ILBC 规范> https://www.cnblogs.com/KSongKing/p/10354824.htm 发起这个项目的原因是, 本来想用 VsCode 来写 ...
- Creating Excel files with Python and XlsxWriter(通过 Python和XlsxWriter来创建Excel文件(xlsx格式))
以下所有内容翻译至: https://xlsxwriter.readthedocs.io/ #----------------------------------------------------- ...
- HTTPS SSL & TLS
HTTPS (HTTP Secure) is an adaptation of the Hypertext Transfer Protocol (HTTP) for secure communicat ...
- oracle 查询所有约束
主键约束SELECT USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约束名, USER_CONS_COLUMNS.TABLE_NAME AS 表名, USER_CON ...
- VS2017编译GDAL(64bit)+解决C#读取Shp数据中文路径的问题
编译GDAL过程比较繁琐,查阅了网上相关资料,同时通过实践,完成GDAL的编译,同时解决了SHP数据中文路径及中文字段乱码的问题,本文以“gdal-2.3.2”版本为例阐述整个编译过程. 一.编译准备 ...
- docker18.09.5 Dockerfile文件编写
Dockerfile命令详解(超全版本) https://www.cnblogs.com/dazhoushuoceshi/p/7066041.html 案例1 dockerfile文件内容: FRO ...