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 ...
随机推荐
- 猴子分桃—Python
def f(): for i in range(3120,4000): flag = 1 k=i for j in range(5): if i%5==1: i=(i//5)*4 else: flag ...
- obs源码uml
- python selenium-webdriver 下拉菜单处理 (九)
测试过程中经常遇到下来菜单,比如说分页,每页显示的条数,以及语言的切换,很多时候经常是以下来菜单的形式展现,下面我们看一下selenium如何处理下来菜单. 首先selenium 很人性化的给提供了一 ...
- MySQL更改命令行默认分隔符
MySQL命令行默认语句分隔符为分号 ; 使用DELIMITER命令可以更改默认分隔符 mysql> DELIMITER // 将默认分割符改为 //
- Docker外包团队 2019年3月更新 企业如何使用Docker
很难将Docker所带来的影响统一的用一种特质来说.当使用Docker执行好时,它对组织,团队,开发者以及运维人员有多层次的好处.Docker使得架构设计简单化,因为所有的应用都将一致的从外部来透视主 ...
- 闲话Pipeline In Maya
在整个行业都在高呼“农业学大寨,流程学xx”的大背景下,你想推出一个新的更好的流程有着极大的难度. 在2014年的时候行业内大部分公司就有了资产的概念,会成立资产部门去专门创建资产,供后续环节多次重用 ...
- mysql 设置初始密码
mysqladmin -uroot password "123" 设置初始密码 由于原密码为空,因此-p可以不用 mysqladmin -uroot -p"123&quo ...
- MSIL 教程
Microsoft Intermediate Language (MSIL) is a language used as the output of a number of compilers (C# ...
- Tree命令使用
Tree命令使用 格式:tree + 参数 tree命令行参数: -a 显示所有文件和目录. -A 使用ASNI绘图字符显示树状图而非以ASCII字符组合. -C 在文件和目录清单加上色彩,便于区分各 ...
- JavaScript 高级特性
1. 原型Prototype 1.1 构造函数 所谓"构造函数",其实就是一个普通函数,但是内部使用了this变量.对构造函数使用new运算符,就能生成实例,并且this变量会绑定 ...