题目

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;
}}
代码下载:https://github.com/jimenbian/GarvinLeetCode

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

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

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

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

【LeetCode从零单排】No15 3Sum相关推荐

  1. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  2. 【LeetCode从零单排】No70.ClimbingStairs

    题目 爬楼梯问题,这是一道很有趣的问题.首先看题目: You are climbing a stair case. It takes n steps to reach to the top. Each ...

  3. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  4. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

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

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

  6. 【LeetCode从零单排】No22.Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  7. 【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 ...

  8. 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

    背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...

  9. 【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 ...

最新文章

  1. 引用log4j.jar包后,出现告警
  2. matlab最小错误率决策,利用MATLAB实现最小错误率贝叶斯判别
  3. Kafka精华问答 | Kafka的主要应用场景有哪些?​
  4. 关于@Import注解的几个问题
  5. silverlight经典案例
  6. 虚拟ip工具_《跟唐老师学习云网络》 - ip命令
  7. 详细解读Spark的数据分析引擎:Spark SQL
  8. 下载android 源码对应的kernel版本
  9. 尼尔机械纪元 机器人博士_还记得章鱼博士吊打蜘蛛侠的炫酷机械臂吗?现实版已经研发出来了...
  10. MATLAB连接comsol后MATLAB语言方法
  11. 一些特殊字符(如:←↑→↓等箭头符号)的Unicode码值
  12. 笔记73-应用容器Docker
  13. java计算机毕业设计消防网站源代码+数据库+系统+lw文档
  14. 计算机图形学——三维图形几何变换和投影转换(VC)
  15. Illustrator 教程:如何在 Illustrator 中应用任意形状渐变?
  16. nds linux模拟器下载,NDS模拟器
  17. UVA 11549 模拟 Floyed判圈法的应用 Calculator Conundrum
  18. 首届中国国际新型储能技术及工程应用大会今日在长沙召开
  19. 全角转半角与半角转全角
  20. 解决大型企业项目规划难题,「ONES Plan 项目集管理」重磅发布

热门文章

  1. python load_Python实例:numpy.load()的使用
  2. linux tr 变量大小写,使用tr命令快速达到大小写互换——深圳培训linux
  3. mysql停止主从_不停止mysql服务配置主从
  4. Shell中的分支语句
  5. 用php画一个蓝底红色的圆_php把图片处理成圆形透明的头像
  6. 云服务器 cvm操作系统选择,云服务器cvm操作系统选择
  7. pyspark DataFrame 转RDD
  8. 47. Leetcode 107 - 二叉树的层次遍历 ii (二叉树-二叉树遍历)
  9. Leetcode 33.搜索旋转排序数组 (每日一题 20210707)
  10. pyechart 应用: graph