【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岁,普通院校本科,计算机专业.大学之前对计算机编程没有一点涉及.大学学计算机专业也是个偶然.因为当初高考的成绩不好,结果都是我父亲帮我报的学校和专业. 上了大学之后,大一都是在新奇中度过 ...
随机推荐
- android平台TextView使用ImageSpan画廊GIF图像
android-gif-drawable(https://github.com/koral--/android-gif-drawable/releases)开源项目---是一个蛮不错的android ...
- memcached 分布式聚类算法
memcached 分布式集群,该决定必须书面开发商自己.和redis 由分布式server决定.上 memcached 有两个选项用于分布式.第一个是:模运算 另一种是:一致性hash 分布式算法. ...
- Web服务器Raspkate的RESTful API
基于轻量型Web服务器Raspkate的RESTful API的实现 在上一篇文章中,我们已经了解了Raspkate这一轻量型Web服务器,今天,我们再一起了解下如何基于Raspkate实现简单的RE ...
- 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...
- Coco2dx-3.0中怎样调用LUA
一个用3.0的工具导出类到lua,自己主动生成代码的方法. 曾经要导出c++类到lua.就得手动维护pkg文件,那简直就是噩梦.3.0以后就会感觉生活非常轻松了. 以下我就在说下详细做法. 1.安装必 ...
- 0x00000000该内存不能为read
0X000000存储器不能read解决方案 有这种现象方面,首先,在硬件,这有问题的内存,二,软件,其中有许多问题. 一:先说说硬件: 一般来说,电脑硬件不easy生病.内存故障的可能性并不大(非你的 ...
- Linux下常用操作汇总
查看linux操作系统位数 (1) 终端输入: file /sbin/init 如 显示: /sbin/init: ELF 32-bit LSB executable, Intel 80386, ve ...
- linux根据该文件夹的读取权限和权限运行差异
假设你linux下使用ls.细心的你会发现居然夹有权限运行.例如: drwxrwxr-x 11 cl cl 4096 9 25 14:22 ./ drwxr-xr-x 49 cl cl 4096 1 ...
- 乐在其中设计模式(C#) - 外观模式(Facade Pattern)
原文:乐在其中设计模式(C#) - 外观模式(Facade Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 外观模式(Facade Pattern) 作者:webabcd 介绍 ...
- Windows 7 USB DVD Download Tool 制作的U盘无法启动安装Windows7 SP1
以前用此工具安装Windows7一直正常,未遇到不能启动安装的问题.Windows7 SP1出来后,用此工具制作安装多台机器均不能引导安装(品牌机和兼容机均是如此 ),要么停留在光标闪烁的状态,要么就 ...