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 ...
随机推荐
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
- -_-#【缓存】Content-Type 错误
页面做了缓存.手机端访问后 Type 变成了 text/vnd.wap.wml.
- ZOJ 3675 Trim the Nails
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 DP+状态压缩. http://www.cnblogs.com/dgsr ...
- SoupUI学习资料
官网: https://www.soapui.org 下载地址: https://www.soapui.org/downloads/soapui.html 官方文档: https://www.soap ...
- bootstrap 仿实例
bootstrap实现一个网页 html文件 <!DOCTYPE html> <html> <head lang="en"> <meta ...
- oracle 零散知识汇集
1. Select '登陆' + 2 From dual会报错: ora- 01722 无效数字,原理是oracle把'登陆'当成数字来和2进行加法运算. Select '登陆'|| 2 From d ...
- Sql Server 2005 CLR实例
本文转载:http://www.cnblogs.com/yongfa365/archive/2010/04/26/SQL-Server-CLR.html CSDN:博客参考http://blog.cs ...
- [转载]Android利用convertView优化ListView性能
本的getView写法 Java代码public View getView(int position, View convertView, ViewGroup parent) {View view = ...
- 浙江大学PAT上机题解析之1015. 德才论 (25)
宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人 ...
- CSDN Markdown简明教程5-高速上手
0.文件夹 文件夹 前言 CSDN Markdown特点 CSDN Markdown高速上手 1 使用快捷键 粗体斜体 引用 链接 高亮代码块 图片 标题 列表 切割线 撤销反复 2 使用离线写作 3 ...