称号

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. NYOJ 47 河问题

    时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 在漆黑的夜里,N位旅行者来到了一座狭窄并且没有护栏的桥边.假设不借助手电筒的话,大家是不管怎样也不敢过桥去的.不幸的是 ...

  2. Qt 3D研究(九):尝试第二边缘检测方法

    Qt 3D研究(九):尝试第二边缘检测方法 三维应用程序,通过FBO.将3D图像渲染成纹理,然后对渲染成的纹理进行图像处理,终于显示在屏幕上的.是风格化后的图案.上一次我使用了一种普通的图像处理方法: ...

  3. freemarker 里 ?? 和 ? 都是什么意思

    ??是推断对象是否为空,比如:<#if object??>object对象不为空(即object存在)</#if> ?后面要加keyword,比如:<#if object ...

  4. BDB (Berkeley DB)简要数据库(转载)

    使用最近DBD.然后搜了下相关资料,首先公布的是一门科学: 转会http://www.javaeye.com/topic/202990 DB综述DB最初开发的目的是以新的HASH訪问算法来取代旧的hs ...

  5. hibernate 基本和简单易用

    首先hibernate.cfg.xml构造,在该文件src文件夹 <!DOCTYPE hibernate-configuration PUBLIC               "-// ...

  6. 设计模式 - Abstract Factory模式(abstract factory pattern) 详细说明

    Abstract Factory模式(abstract factory pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/ ...

  7. Reveal:分析iOS UI该武器

    Reveal是分析iOS应用UI的利器: Reveal可以在执行时调试和改动iOS应用程序.它能连接到应用程序,并同意开发人员编辑各种用户界面參数.这反过来会马上反应在程序的UI上.就像用FireBu ...

  8. 乐在其中设计模式(C#) - 组合模式(Composite Pattern)

    原文:乐在其中设计模式(C#) - 组合模式(Composite Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 组合模式(Composite Pattern) 作者:weba ...

  9. 认识mongoDB数据库

    mongodb中有三元素:数据库,集合,文档,其中“集合”对应关系型数据库中的“表”,“文档”对应“行”. 安装mongoDB: 去官网下载对应系统的mongoDB压缩包,解压后将文件夹重命名为mon ...

  10. 让你提前认识软件开发(17):makefile文件的书写及应用

    第1部分 又一次认识C语言 makefile文件的书写及应用 [文章摘要] makefile用于Linux下整个project的编译.对于Linux下的C/C++语言的编译是至关重要的. 本文以实际的 ...