3Sum

Given an array S of n integers, are there elements abc 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)
 
先对数组进行排序,从头开始扫描,扫描的数作为固定的数。
a=num[i];
 
当固定了一个数以后,
选取b=num[i+1]=num[j]
选取c=num[n-1]=num[k]
 
 
若a+b+c=0
则找到了一个a,b,c,记录下来,j++,k--继续扫描
若a+b+c<0
则j++
若a+b+c>0
则k--
 
 
要注意去重的问题:
1.若num[i]=num[i-1],则跳过
2.若num[j]=num[j-1],则跳过
3.若num[k]=num[k+1],则跳过
 
 
 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的更多相关文章

  1. 【LeetCode】3Sum 解决报告

    这个问题是我目前的知识回答,不来,只有良好的网上搜索解决方案,发现 K Sum 它是一类问题,但是,互联网是没有更简洁的代码,我想对于谁刚开始学习的人.您可能仍然想看看这个问题该怎么解决,然后看看他们 ...

  2. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

  3. 【leetcode】3Sum Closest

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  4. 【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 ...

  5. 【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 ...

  6. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  7. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  8. 【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  ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. Spring-事物传播行为

    spring事物的传播属性(7种) REQUIRED(默认) 业务方法需要在一个容器里运行.如果方法运行时,已经处在一个事务中,那么加入到这个事务,否则自己新建一个新的事务. 存在事物,则使用当前事物 ...

  2. Java虚拟机的功能

    1:通过ClassLoader寻找和装载class文件 2:解释字节码成为指令并执行,提供class文件的运行环境.即将字节码转换为不同OS下可执行的机器码指令. 3:进行垃圾回收. 4:提供与硬件交 ...

  3. 16.(转) Android之Support v4、v7、v13的区别和应用场景

    我们在项目中经常会碰到Android Support v4.v7和v13包兼容问题,所以有必要梳理下这些东西. google提供了Android Support Library package 系列的 ...

  4. POJ3169 Layout

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  5. BZOJ2818 欧拉函数

    题意:求1--n中满足gcd(x,y)的值为质数的数对(x,y)的数目 ( (x,y)和(y,x)算两个 ) sol: 设p[i]是一个质数,那么以下两个命题是等价的: 1.gcd(x,y)=1 2. ...

  6. 如果您想省略JS里的分号,了解一下JS的分号插入原理吧

    仅在}之前.一个或多个换行之后和程序输入的结尾被插入 也就是说你只能在一行.一个代码块和一段程序结束的地方省略分号. 也就是说你可以写如下代码 function square(x) { var n = ...

  7. 搭建的SSH 框架

    公用JDBC 方法,如果要保存数据,不许再service 中写,而且必须带save*  update* 的方法名才受事物控制,ajax 返回json 控制,登录拦截器, 用户体系我没有建立,个人需要不 ...

  8. The Joys of Conjugate Priors

    The Joys of Conjugate Priors (Warning: this post is a bit technical.) Suppose you are a Bayesian rea ...

  9. Openresty 与 Tengine

    Openresty 与 Tengine Openresty和Tengine基于 Nginx 的两个衍生版本,某种意义上他们都和淘宝有关系,前者是前淘宝工程师agentzh主导开发的,后者是淘宝的一个开 ...

  10. [Asp.Net]状态管理(Session、Application、Cache)

    上篇博文介绍了在客户端状态管理的两种方式:http://www.cnblogs.com/wolf-sun/p/3329773.html.除了在客户端上保存状态外,还可以在服务器上保存状态.使用客户端的 ...