【LeetCode从零单排】No15 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)
代码
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的更多相关文章
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
		
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
 - 【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 ...
 - 【LeetCode从零单排】No.135Candy(双向动态规划)
		
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
 - 【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 ...
 - HDU4870_Rating_双号从零单排_高斯消元求期望
		
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
 - 从零单排Linux – 3 – 目录结构
		
从零单排Linux – 3 – 目录结构 1.FHS标准(filesystem hierarchy standard) why? –> 为了规范,还有为了linux的发展 重点 –> 规范 ...
 - 从零单排Linux – 2 – 目录权限
		
从零单排Linux – 2 – 目录权限 1.sync 讲内存数据跟新到硬盘中 2.执行等级init a: run level 0:关机 b: run level 3:纯命令模式 c:run leve ...
 - 从零单排Linux – 1 – 简单命令
		
从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...
 - JAVA从零单排之前因
		
本人,男,21岁,普通院校本科,计算机专业.大学之前对计算机编程没有一点涉及.大学学计算机专业也是个偶然.因为当初高考的成绩不好,结果都是我父亲帮我报的学校和专业. 上了大学之后,大一都是在新奇中度过 ...
 
随机推荐
- uvalive4015 (树上背包)
			
给一棵树,边上有权值,然后给一个权值x,问从根结点出发, 走不超过x的距离,最多能经过多少个结点. 走过的点可以重复走,所以可以从一个分支走下去,然后走回来,然后再走另一个分支 dp[u][j][0] ...
 - 【翻译】Why JavaScript Is and Will Continue to Be the First Choice of Programmers
			
花费2半小时,那么最终会被翻译.假设有问题,请提出,毕竟,自己的6不超过级别. 附加链接 Why JavaScript Is and Will Continue to Be the First Cho ...
 - AMD宣布裁员7% 约710员工将失去工作
			
10 月 17 日.美国芯片生产商 AMD 周四宣布将裁员7%.并公布了不及预期的第四季度业绩展望.这将是 AMD 自 2011 年以来的第三轮大裁员. 就在一周之前,AMD 宣布罗瑞德(Rory R ...
 - Objective C (iOS) for Qt C++ Developers(iOS开发,Qt开发人员需要了解什么?)
			
Qt/C++开发人员眼中的Obj-C 对于我们第一次自己定义iOS应用来说,对于来自Qt/C++开发人员来说,我不得不学习Objective-C相关语法与知识 为了让读者可以更easy理解这 ...
 - javaweb学习总结(七)——HttpServletResponse对象(一)(转)
			
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应,那我们 ...
 - C++学习笔记33  转换操作符
			
有时候,我们要转换为类类型和类类型,同时,这两个类继承关系不存在,这时候我们就需要一些所谓的转换操作符运营商. 一个简单的例子.类别A转换为int种类 #include <iostream> ...
 - android 网络运营商的名字显示规则(锁定屏幕,下拉列表)
			
一:Background & 有关flow MTK Operator name display分为两种类型的手机: 1. Sim卡名称: 从基于引导SIM卡读取IMSI到Spn-conf.xm ...
 - 树莓派安装 dig命令
			
apt-get install dnsutils
 - C++ 习题 输出日期时间--友元类
			
Description 设计一个日期类和时间类,编写display函数用于显示日期和时间.要求:将Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据, ...
 - ASP.NET执行循序
			
首先第一次运行一个应用程序(WebSite或者WebApplication都是Web应用程序)第一次请求 -> 1,IIS -> 2,aspnet_isapi(非托管dll) -> ...