15 3Sum(寻找三个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列
思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i、j、k
对于i指针从前往后遍历,对于一个固定的i指针,其实就是2Sum的情况,给定一前一后两个指针进行遍历,
值大了,就把后面的指针往前移,值小了就把前面的指针往后移。
比较麻烦的地方在于去重,首先是i指针的去重,j和k只用一个去重就可以了
ps:这是数组中一种十分常用的方法。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> temp();
int size=nums.size();
int j,k;
sort(nums.begin(),nums.end());
for(int i=;i<size-;++i){ //注意这种位置可以写nums.size(),就是不能写nums.size()-2,什么原因我还没搞明白
//while(i>0&&nums[i]==nums[i-1])++i; 注意比较两种写法
if(i>&&nums[i]==nums[i-])continue;
j=i+;
k=size-;
while(j<k){
if(j>i+&&nums[j]==nums[j-]){
++j;
continue;
}
if(nums[j]+nums[k]>-nums[i])--k;
else if(nums[j]+nums[k]<-nums[i])++j;
else{
temp[]=nums[i];
temp[]=nums[j];
temp[]=nums[k];
ans.push_back(temp);
++j;
--k;
}
}
}
return ans;
}
};
复杂度:O(n2)
15 3Sum(寻找三个数之和为指定数的集合Medium)的更多相关文章
- 18 4Sum(寻找四个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为target的所有情况,这些情况不能重复,增序排列 思路:采用3Sum的做法 ps:有见一种用hash的,存任意两个元素的和,然后变成3sum问题,需要 ...
- 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 ...
- 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 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- JS使用三元运算符判断三个数中最大的数
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [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 16 3Sum Closest (最接近target的3个数之和)
题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description Problem : 找到给定数组中a+b+c 最接近targe ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- Java [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 ...
随机推荐
- 【线段树】HDU 5493 Queue (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5493 题目大意: N个人,每个人有一个唯一的高度h,还有一个排名r,表示它前面或后面比它高的人的个数 ...
- 【字符串】【最小表示法】Vijos P1683 有根树的同构问题
题目链接: https://vijos.org/p/1683 题目大意: 给M棵树,每棵N个点,N-1条边,树边有向,问哪些树同构. 题目思路: [字符串][最小表示法] 用()表示一个节点,那么三个 ...
- cf703A Mishka and Game
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- J - Fire!
题目大意: 这是一个放火逃生的游戏,就是给出来一个迷宫,迷宫里面有人‘J’和火焰‘F’当然这些火焰可能不止一处,然后问这个人最快从迷宫里面逃出来需要多久 /////////////////////// ...
- 解决拼团首页swiper组件手动轮播卡顿问题
解决 swiper lag , 可能是渲染背景backface-visibility后导致卡顿吧! //以下代码添加到.swiper-wrapper中 -webkit-perspective: 300 ...
- IOS中内存管理机制浅解
我们知道在程序运行过程中要创建大量的对象,和其他高级语言类似,在ObjC中对象时存储在堆中的,系统并不会自动释放堆中的内存(注意基本类型是 由系统自己管理的,放在栈上).如果一个对象创建并使用后没有得 ...
- Gradle 1.12 翻译——第十三章 编写构建脚本
有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...
- SVN Checkout 不包括源文件夹根目录(转)
SVN Checkout 不包括源文件夹根目录,比如我要checkout trunk/ 下面的所有文件,但是不包括trunk 文件夹 我们可以在svn文件夹后面打个空格,在加个“.”就行了 eg: ...
- MYSQL 体系结构图-LRU
- Percona Live 2016 PPT整理-zhaiwx_yinfeng
https://yq.aliyun.com/articles/54454?spm=5176.100239.bloglist.32.CllwIr