【LeetCode】三数之和【排序,固定一个数,然后双指针寻找另外两个数】
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum
分析:
暴力法的复杂度是O(N^3),肯定会超时
先将数组排序,假设k1<=k2<=k3,先固定k1,然后通过双指针法在k1的后面寻找k2和k3
可以利用的性质:
1.如果k1大于0,则三数之和肯定无法等于0,可以跳过该k1
2.如果v[i]==v[i-1],那么i-1可以跳过,因为这样必然会导致重复结果
可以采用set去除重复结果
时间复杂度:O(N^2)
排序的复杂度是O(N*log N),但是后续固定看+双指针的复杂度是O(N^2),二者取大的,所以时间复杂度是O(N^2)
空间复杂度:快排需要,最好情况:O(log N),最坏情况:O(N)
code:
class Solution {
public:
vector<vector<int> > threeSum(vector<int>& a)
{
vector<vector<int> > vv;
vector<int> v;
set<vector<int> > s;
set<vector<int> >::iterator it;
int n=a.size();
if(n<)
return vv;
sort(a.begin(),a.end());
for(int k=;k<n-;k++)
{
if(a[k]>)
continue;
if(k>&&a[k-]==a[k])
continue;
int l=k+;
int h=n-;
//cout<<"k="<<k<<"l="<<l<<" h="<<h<<endl;
s.clear();
while(l<h)
{
int ans=a[k]+a[l]+a[h];
//cout<<"l="<<l<<" h="<<h<<"ans="<<ans<<endl;
if(ans==)
{
v.clear();
v.push_back(a[k]);
v.push_back(a[l]);
v.push_back(a[h]);
s.insert(v);
l++;
h--;
}else if(ans<)
{
l++;
}else if(ans>)
{
h--;
}
}
if(s.size()!=)
{
for(it=s.begin();it!=s.end();it++)
{
vv.push_back(*it);
}
}
}
return vv;
}
};
【LeetCode】三数之和【排序,固定一个数,然后双指针寻找另外两个数】的更多相关文章
- LeetCode 三数之和 — 优化解法
LeetCode 三数之和 - 改进解法 题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复 ...
- [leetcode]三数之和
三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- leetcode16 最接近的三数之和
做了几周的hard之后,这道题居然轻易就解出来了,稍微debug了一下就ac了,算是有了一丢丢提高把: 思路 这道题因为和三数之和很像,所以充分利用双指针的思想:先排序,然后再固定一个数i,i取值从[ ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- [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题:三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况
Given an integer array A, and an integer target, return the number of tuples i, j, k such that i &l ...
- [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 ...
随机推荐
- 实训作业5(lang、util)
实验内容和原理: 1.将布尔型.整型.长整型.双精度型作为参数,实例化相应的包装类对象,并输出对象的数值. package 包装; public class integer { public stat ...
- 微信小程序路由带参
通过url带参传递 wxml <navigator url="../user/user?id={{item.id}}&name={{item.name}}">点 ...
- js之大文件分段上传、断点续传
文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...
- Centos 不重启 修改ulimit参数
1. 查看limits.conf文件 cat /etc/security/limits.conf 2. 打开编辑limits.conf文件 sudo vim /etc/security/limits. ...
- 【CSP模拟赛】Confess(数学 玄学)
题目描述 小w隐藏的心绪已经难以再隐藏下去了.小w有n+ 1(保证n为偶数)个心绪,每个都包含了[1,2n]的一个大小为n的子集.现在他要找到隐藏的任意两个心绪,使得他们的交大于等于n/2. 输入描述 ...
- OpenFOAM计算结果转换到CFD-Post当中处理
我们编写如下的Python代码 源代码:
- linux df 日志删除命令分析
在部署文件的时候 发现 文件太多了,需要删除: 使用命令行df -h; [sankuai@set-gh-qcs-regulation-wanganbu-test01 com.sankuai.qcs.r ...
- 作业——11 分布式并行计算MapReduce
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapRedu ...
- ArgumentException: The Assembly Mono.WebBrowser is referenced by System.Windows.Forms ('Assets/Plugins/System.Windows.Forms.dll'). But the dll is not allowed to be included or could not be found.
最近有个项目要用到System.Windows.Forms.dll,在Unity编辑器里用着还好好的,但是一导出就给我报错,让我十分不爽. 于是请教百度,搜出了五花八门的答案,没一个能解决我的问题的, ...
- freemarker null异常详解及兼容模式
在读取user的时候,因为为空,报错了,错误处的代码是这样的 <#if user> 其实准确的写法应该是 <#if user??> 如果要消除错误,需要把前端代码修后成后面这种 ...