【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 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.
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
class Solution {
public:
vector<vector<int> > threeSum(vector<int> &num) {
int n=num.size();
sort(num.begin(),num.end());
int i,j,k;
int a,b,c;
vector<vector<int> > result;
for(i=;i<n-;i++)
{
j=i+;
k=n-;
while(j<k)
{
a=num[i];
if(i>&&num[i]==num[i-])
{
break;
}
b=num[j];
if(j>i+&&num[j]==num[j-])
{
j++;
continue;
}
c=num[k];
if(k<n-&&num[k]==num[k+])
{
k--;
continue;
}
if(a+b+c==)
{
vector<int> tmp();
tmp[]=a;
tmp[]=b;
tmp[]=c;
result.push_back(tmp);
j++;
k--;
}
else if(a+b+c<)
{
j++;
}
else if(a+b+c>)
{
k--;
}
}
}
return result;
}
};
【leetcode】3Sum的更多相关文章
- 【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】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 (medium)
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】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】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- 【LeetCode】4Sum 解题报告
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- MyEclipse 开发 Web项目发布到 Tomcat 下的Root 目录
通常情况下,Web项目是发布到Tomcat下的webapps文件目录下的 . 例如:Web应用项目名称为:stock,则部署到tomcat后,是部署在tomcat/webapps/stock中,网址为 ...
- 【POJ 1094】拓扑排序
题意 给出n,代表有以A开始的n个字母,给出它们的m个小于关系(A<B).如果前i个关系可以确定n个字母的一个顺序就输出: Sorted sequence determined after i ...
- 解决:Angular-cli:执行ng-build --prod后,dist文件里无js文件、文件未压缩等问题
Angular2.0于2016年9月上线,我于9月入坑. 入坑以来,一直让我很困惑的问题 1.angular-cli是个什么鬼东西? 2.为什么我们自己的资源文件还没写什么,就有起码50多个js文件加 ...
- DEDECMS数据库执行原理、CMS代码层SQL注入防御思路
我们在上一篇文章中学习了DEDECMS的模板标签.模板解析原理,以及通过对模板核心类的Hook Patch来对模板的解析流量的攻击模式检测,达到修复模板类代码执行漏洞的目的 http://www.cn ...
- ajax提交特殊字符的处理
前台页面用encodeURIComponent()这个js方法 rule=encodeURIComponent(rule);//对特殊字符编码 后台页面 java.net.URLDecoder url ...
- VMware 11安装Mac OS X 10.11.5虚拟机以及优化心得
随着苹果WWC大会退出了MAC最新版的10.11.5,本着一颗“极客”的心情,在第一时间用VMWARE虚拟机装上了.然后各种卡顿这里分享一下优化mac虚拟机的心得. 1 从Dock上移除Dashboa ...
- POJ2586Y2K Accounting Bug(贪心 + 不好想)
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12251 Accepted: 62 ...
- ASP.NET MVC ActionFilterAttribute的执行顺序
http://diaosbook.com/Post/2014/6/3/execution-order-of-actionfilter-aspnet-mvc ASP.NET MVC里面我们要自定义Act ...
- SmartImageView&常见的开源代码
1)说明: 该控件实现图片的显示----网络路径也可以显示出来---加载完成之后 就可以 缓存到内存里面!
- linux磁盘空间清理
由于当初安装系统设计不合理,有些分区的过小,以及网络通讯故障等造成日志文件速度增长等其他原因都可以表现为磁盘空间满,造成无法读写磁盘,应用程序无法执行等.下面就给你支几招(以/home空间满为例): ...