【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从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No70.ClimbingStairs
题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
- 【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似 ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【LeetCode从零单排】No221.Maximal Square
题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
最新文章
- 引用log4j.jar包后,出现告警
- matlab最小错误率决策,利用MATLAB实现最小错误率贝叶斯判别
- Kafka精华问答 | Kafka的主要应用场景有哪些?​
- 关于@Import注解的几个问题
- silverlight经典案例
- 虚拟ip工具_《跟唐老师学习云网络》 - ip命令
- 详细解读Spark的数据分析引擎:Spark SQL
- 下载android 源码对应的kernel版本
- 尼尔机械纪元 机器人博士_还记得章鱼博士吊打蜘蛛侠的炫酷机械臂吗?现实版已经研发出来了...
- MATLAB连接comsol后MATLAB语言方法
- 一些特殊字符(如:←↑→↓等箭头符号)的Unicode码值
- 笔记73-应用容器Docker
- java计算机毕业设计消防网站源代码+数据库+系统+lw文档
- 计算机图形学——三维图形几何变换和投影转换(VC)
- Illustrator 教程:如何在 Illustrator 中应用任意形状渐变?
- nds linux模拟器下载,NDS模拟器
- UVA 11549 模拟 Floyed判圈法的应用 Calculator Conundrum
- 首届中国国际新型储能技术及工程应用大会今日在长沙召开
- 全角转半角与半角转全角
- 解决大型企业项目规划难题,「ONES Plan 项目集管理」重磅发布
热门文章
- python load_Python实例:numpy.load()的使用
- linux tr 变量大小写,使用tr命令快速达到大小写互换——深圳培训linux
- mysql停止主从_不停止mysql服务配置主从
- Shell中的分支语句
- 用php画一个蓝底红色的圆_php把图片处理成圆形透明的头像
- 云服务器 cvm操作系统选择,云服务器cvm操作系统选择
- pyspark DataFrame 转RDD
- 47. Leetcode 107 - 二叉树的层次遍历 ii (二叉树-二叉树遍历)
- Leetcode 33.搜索旋转排序数组 (每日一题 20210707)
- pyechart 应用: graph