【leetcode】3Sum (medium)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
思路:
把最小值从头到尾循环,中间值和最大值两边收缩。
我写的时候是在最后去重复,总是超时。后来看了人家的答案,发现可以每次对最小、中间、最大值去重。就可以AC了
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
vector<vector<int> > ans;
if(num.size() < )
{
return ans;
}
int small = ;
int big = num.size() - ;
int mid = ;
int sum = ;
sort(num.begin(), num.end());
for(small = ; small < num.size() - ; /*small++*/) //最小数字循环 中间与最大数字两边收缩
{
mid = small + ;
big = num.size() - ;
while(mid < big)
{
sum = num[small] + num[mid] + num[big];
if(sum > )
{
big--;
}
else if(sum < )
{
mid++;
}
else
{
vector<int> v;
v.push_back(num[small]);
v.push_back(num[mid]);
v.push_back(num[big]);
ans.push_back(v);
do { mid++; }while (mid < big && num[mid - ] == num[mid]); //注意!!
do { big--; }while (mid < big && num[big + ] == num[big]);//注意!!
}
}
do{ small++; }while (small < num.size() - && num[small - ] == num[small]);//注意!!
}
return ans;
}
}; int main()
{
Solution s;
vector<int> num;
num.push_back(-);
num.push_back(-);
num.push_back(-);
num.push_back(-);
num.push_back(-);
num.push_back();
num.push_back();
num.push_back();
num.push_back();
num.push_back(); vector<vector<int>> ans = s.threeSum(num); return ; }
【leetcode】3Sum (medium)的更多相关文章
- 【LeetCode】3Sum 解决报告
这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】3Sum Closest
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 【leetcode】3Sum
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...
- 【leetcode】3Sum Closest(middle)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
随机推荐
- [百度地图] ZMap 与 MultiZMap 封装类说明;
ZMap.js 与 MultiZMap 说明 1. ZMap 与 MultiZMap 都是封装一些地图常用的使用方法,类方法功能大多使用 prototype 原型 实现: ZMap 在一个页面只能使用 ...
- 【CISP笔记】安全漏洞与恶意代码(1)
恶意代码 类型二进制代码.脚本语言.宏语言等表现形式病毒.蠕虫.后门程序.木马.流氓软件.逻辑炸弹等 恶意代码的传播方式(1)移动存储 自动播放功能.Windows默认.自动执行autorun.inf ...
- IIS7.5 在已有的WEB网站上配置FTP发布
IIS7.5 有了很多新特性,例如FashCGI,Rewrite 模块的内置,简易的FTP发布等等,但是即使是微软,也没有详细的文档,本文详细的介绍了如何在现有的WEB网站上建立FTP发布. IIS ...
- Java并发包源码学习之AQS框架(四)AbstractQueuedSynchronizer源码分析
经过前面几篇文章的铺垫,今天我们终于要看看AQS的庐山真面目了,建议第一次看AbstractQueuedSynchronizer 类源码的朋友可以先看下我前面几篇文章: <Java并发包源码学习 ...
- hash-2.hashMap
1.HashMap的数据结构 a.HashMap是一个链表散列的结合体,即,数组和链表的结合体. b.HashMap类中定义了Entry类型的数组,Entry [ ] ,Entry有key value ...
- mongodb windows install &python
安装mongo http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ 其中修改: echo logpath=C:\mo ...
- CSS3圆角边框的使用-遁地龙卷风
0.快速入门 border-radius:50px; 1.border-radius详解 border-radius:50px; 上右下左,水平和垂直距离都是50px border-radius:50 ...
- (原创)Activity启动模式之singleTask
android中activity有四种启动模式 standard(默认):在同一个任务栈中可以有重复的activity,基本没什么限制 singleTop:只有当此activity在栈顶时,去创建它, ...
- Struts2 Action 动态传参数
Struts2的两个Action之间传参的问题. 需求功能是这样:Action1 获取数据库配置内容,得到相应Model的 动态URL ,这里的URL 有的是Action有的是JSP页面. 1.使用r ...
- Asp.net MVC网站的基本结构
Asp.net MVC网站的基本结构:Controller->IBLL(定义业务接口)->BLL->DA(调用Utility)->数据库 ,其中A->B表示A调用B 1. ...