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 ...
随机推荐
- maven多模块项目构建
描述 一个大的企业级项目通常跨越了数十万行代码,牵涉了数十或数百软件人员的努力.如果开发者在同一个项目下开 发,那么项目的管理.构建将会变得很难控制.因此设计人员会将项目划分为多个模块,多个模块独 ...
- golang-http-post
func httpPost() { resp, err := http.Post("https://www.abcd123.top/api/v1/login", "app ...
- bootstrap顶部导航遮挡下面内容的解决办法
使用bootstrap设置顶部导航,并将导航栏固定,代码如下: <nav class="navbar navbar-expand-lg navbar-light bg-light fi ...
- Ansible 之动态Inventory文件(二)
上篇主要讲解了Ansible 的安装和配置,并且根据不同的业务场景将服务器的信息存放在Ansible的Inventory中,其实存放这样的数据每次更新都需要我们自动的添加和删除,这样对于我们维护起来很 ...
- Linux下MySQL编码的修改
默认登录mysql之后可以通过SHOW VARIABLES语句查看系统变量及其值. mysql> show variables like '%character%'; 说明:以下是在Cent ...
- HTML5 Audio(音频)
<audio controls> <source src="horse.ogg" type="audio/ogg"> <s ...
- CentOS 7系统初始化
1. 升级系统 $ yum -y update 2.SELinux设置: 禁用 $ vi /etc/selinux/config 修改 SELINUX=disabled
- C++ 线性搜索算法演示的代码
将做工程过程中比较好的内容做个备份,下边代码段是关于C++ 线性搜索算法演示的代码. #include<iostream>#include<conio> int linears ...
- javaAgent介绍
JavaAgent(转载) http://www.cnblogs.com/diyunpeng/archive/2011/05/26/2057932.html 一文带你了解Java Agent http ...
- 深度优先搜索DFS(一)
实例一 0/1背包问题: 有n件物品,每件物品的重量为w[i],价值为c[i].现在需要选出若干件物品放入一个容量为V的背包中,使得在选入背包的物品重量和不超过容量V的前提下,让背包中的物品 ...