称号

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)

代码

public class Solution {
public List<List<Integer>> threeSum(int[] num) {
Arrays.sort(num);
int a,b,c;
HashSet<List<Integer>> hs=new HashSet();
for(int i=0;i<=num.length-3;i++){
a=num[i];
for(int m=i+1,n=num.length-1; m<n;){
b=num[m];
c=num[n];
if(-a==(b+c)){
List<Integer> ls=new ArrayList<Integer>();
ls.add(a);
ls.add(b);
ls.add(c);
hs.add(ls);
n--;
m++;
}
else if(-a>(b+c)){
m++;
}
else{
n--;
}
}
}
List<List<Integer>> result=new ArrayList(hs);
return result;
} }

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

版权声明:本文博客原创文章,博客,未经同意,不得转载。

【LeetCode从零单排】No15 3Sum的更多相关文章

  1. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  2. 【LeetCode从零单排】No189 .Rotate Array

    称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  3. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  4. 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...

  5. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  6. 从零单排Linux – 3 – 目录结构

    从零单排Linux – 3 – 目录结构 1.FHS标准(filesystem hierarchy standard) why? –> 为了规范,还有为了linux的发展 重点 –> 规范 ...

  7. 从零单排Linux – 2 – 目录权限

    从零单排Linux – 2 – 目录权限 1.sync 讲内存数据跟新到硬盘中 2.执行等级init a: run level 0:关机 b: run level 3:纯命令模式 c:run leve ...

  8. 从零单排Linux – 1 – 简单命令

    从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...

  9. JAVA从零单排之前因

    本人,男,21岁,普通院校本科,计算机专业.大学之前对计算机编程没有一点涉及.大学学计算机专业也是个偶然.因为当初高考的成绩不好,结果都是我父亲帮我报的学校和专业. 上了大学之后,大一都是在新奇中度过 ...

随机推荐

  1. 2012天津E题

    给我们n个坐标点和一个距离d,表示车开m米就没油了. 然后要选几个点建立油站,使得能够从1出发遍历所有的点,然后回到1.  并且规定1这个点必须有油站,在第i个点建立油站的费用是 2^(i-1) 因为 ...

  2. WPF换肤之一:创建圆角窗体

    原文:WPF换肤之一:创建圆角窗体 我们都期望自己的软件能够有一套看上去很吸引人眼球的外衣,使得别人看上去既专业又有美感.这个系列就带领着大家一步一步的讲解如何设计出一套自己的WPF的窗体皮肤,如果文 ...

  3. [背景分离] 识别移动物体基于高斯混合 MOG

    使用很easy,  frame 就是当前帧,  foreground 是取得的, binary 型背景, 0.03是学习速率能够依据实际调整. cv::BackgroundSubtractorMOG ...

  4. 配置jndi服务,javax.naming.NamingException的四种情况

    1.当jndi服务没有启动,或者jndi服务的属性没有设置正确,抛出如下异常: javax.naming.CommunicationException: Can't find SerialContex ...

  5. Java面试题集(136-150)

    摘要:目,尽管仅仅有15道题目.可是包括的信息量还是非常大的,非常多题目背后的解题思路和算法是非常值得玩味的. 136.给出以下的二叉树先序.中序.后序遍历的序列? 答:先序序列:ABDEGHCF.中 ...

  6. C++ Primer 学习笔记_40_STL实践与分析(14)--概要、先来看看算法【上】

    STL实践与分析 --概述.初窥算法[上]     标准库容器定义的操作很少.并没有给容器加入大量的功能函数.而是选择提供一组算法,这些算法大都不依赖特定的容器类型,是"泛型"的. ...

  7. Javascript设计模式系列三

    继承,一个类或对象继承另一个类或对象的三种方法.类式继承.原型式继承.掺元类. 一.类式继承,原型链.Extend函数. <script type="text/javascript&q ...

  8. 智能家居DIY

    近期智能家居比較火,将房子简单改造下,也算体验智能家居. 本文解说的是用无线的方式,长处是:不用改造现有线路,直接安装模块就可以实现想要的功能,花的钱也较少,共六百左右 =============== ...

  9. 生命游戏(两),有一种东西叫CCScrollView

    订婚app要么game'肯定不会陌生:CCScrollView并且CCTableView. 假如我不知道是什么CCScrollView,再看看testcpp要么testlua样品棒. 先说说CCScr ...

  10. fiddler打开后 浏览器就上不了网的解决方法

    fiddler设置一下即可 Tools fiddler options connections 不要选中 Act as system proxy on startup