【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 ...
随机推荐
- Spring-事物传播行为
spring事物的传播属性(7种) REQUIRED(默认) 业务方法需要在一个容器里运行.如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务. 存在事物,则使用当前事物 ...
- Java虚拟机的功能
1:通过ClassLoader寻找和装载class文件 2:解释字节码成为指令并执行,提供class文件的运行环境.即将字节码转换为不同OS下可执行的机器码指令. 3:进行垃圾回收. 4:提供与硬件交 ...
- 16.(转) Android之Support v4、v7、v13的区别和应用场景
我们在项目中经常会碰到Android Support v4.v7和v13包兼容问题,所以有必要梳理下这些东西. google提供了Android Support Library package 系列的 ...
- POJ3169 Layout
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- BZOJ2818 欧拉函数
题意:求1--n中满足gcd(x,y)的值为质数的数对(x,y)的数目 ( (x,y)和(y,x)算两个 ) sol: 设p[i]是一个质数,那么以下两个命题是等价的: 1.gcd(x,y)=1 2. ...
- 如果您想省略JS里的分号,了解一下JS的分号插入原理吧
仅在}之前.一个或多个换行之后和程序输入的结尾被插入 也就是说你只能在一行.一个代码块和一段程序结束的地方省略分号. 也就是说你可以写如下代码 function square(x) { var n = ...
- 搭建的SSH 框架
公用JDBC 方法,如果要保存数据,不许再service 中写,而且必须带save* update* 的方法名才受事物控制,ajax 返回json 控制,登录拦截器, 用户体系我没有建立,个人需要不 ...
- The Joys of Conjugate Priors
The Joys of Conjugate Priors (Warning: this post is a bit technical.) Suppose you are a Bayesian rea ...
- Openresty 与 Tengine
Openresty 与 Tengine Openresty和Tengine基于 Nginx 的两个衍生版本,某种意义上他们都和淘宝有关系,前者是前淘宝工程师agentzh主导开发的,后者是淘宝的一个开 ...
- [Asp.Net]状态管理(Session、Application、Cache)
上篇博文介绍了在客户端状态管理的两种方式:http://www.cnblogs.com/wolf-sun/p/3329773.html.除了在客户端上保存状态外,还可以在服务器上保存状态.使用客户端的 ...