Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: 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]
]

题目标签:Array
  这道题目给了我们一个nums array, 让我们找到所有任意三个数字加起来等于0的可能性,但是不能重复。这道题目我们要运用Two Sum(Two pointers)方法来帮助完成,那么我们首先就要sort array。这样才可以利用Two Sum。也可以帮助避免重复的数字。然后遍历nums array,这里只需要遍历到最后倒数第三个就可以了,因为一共需要三个数字,后面只有两个数字的时候,没必要。对于每一个数字,把它* -1,比如说题目中的例子: -4 -1 -1 0 1 2, 把-4 * -1 = 4, 然后需要在后面的array 里找到两个数字之和等于4的。这样就是在做Two Sum题目了。如果遇到-1 -1这种情况,第一个-1 我们需要,在后面array 里找两数之和等于1的。当遇到第二个重复的,直接skip。同样的,在后面的array里找两数之和的话,遇到第二个重复的也跳过。
 
 

Java Solution:

Runtime beats 82.41%

完成日期:07/11/2017

关键词:Array

关键点:利用TwoSum(two pointers)来辅助

 public class Solution
{
public List<List<Integer>> threeSum(int[] nums)
{
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>(); for(int i=0; i<nums.length-2; i++) // no need to find twoSum if rest array size is only 1 or 0
{
if(i > 0 && nums[i] == nums[i-1]) // if previous num is same, skip this num
continue; // for each num, find the twoSum which equal -num in the rest array
int left = i + 1;
int right = nums.length-1;
int sum = nums[i] * -1; while(left < right)
{
if(nums[left] + nums[right] == sum) // find the two
{
res.add(Arrays.asList(nums[i], nums[left], nums[right])); // ascending order
left++;
right--; while(left < right && nums[left] == nums[left-1]) // if next num is same as this, skip this
left++;
while(left < right && nums[right] == nums[right+1])
right--;
}
else if(nums[left] + nums[right] > sum) // meaning need smaller sum
right--;
else // nums[left] + nums[right] < sum, meaning need larger sum
left++;
} } return res;
}
}

参考资料:

https://discuss.leetcode.com/topic/8125/concise-o-n-2-java-solution

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

 

LeetCode 15. 3Sum(三数之和)的更多相关文章

  1. [LeetCode] 15. 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  2. [leetcode]15. 3Sum三数之和

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  3. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  4. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  5. 【LeetCode 15】三数之和

    题目链接 [题解] 先把n个数字升序排个序. 然后枚举三元组最左边的那个数字是第i个数字. 之后用两个指针l,r移动来获取三元组的第2个和第3个数字. (初始值,l=i+1,r = n-1); 如果a ...

  6. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  7. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  8. LeetCode 第15题-三数之和

    1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...

  9. LeeCode数组第15题三数之和

    题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  10. 【LeetCode每天一题】3Sum(三数之和)

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

随机推荐

  1. 快速搭建一个Fabric 1.0的环境

    之前笔者写了一篇Fabric1.0 Beta的部署和Fabric 1.0的多机部署.但是很多人在部署Fabric的时候还是很容易出问题,所以我就再把Fabric 1.0的单机环境搭建讲一下.其实很多内 ...

  2. LINUX - awk命令之NF和$NF区别 (转)

    NF和$NF 区别问答:(转)1.awk中$NF是什么意思?#pwd/usr/local/etc~# echo $PWD | awk -F/ '{print $NF}'etcNF代表:浏览记录的域的个 ...

  3. panda库------对数据进行操作---合并,转换,拼接

    >>> frame2 addr age name 0 beijing 12 zhang 1 shanghai 24 li 2 hangzhou 24 cao >>> ...

  4. Extjs2.0 desktop 动态创建桌面图标和开始菜单

    这几天一直纠结Extjs desktop怎么动态读取数据,用Ext.net已经实现但是不灵活.Ext.net做出来的桌面在窗口关闭后只是隐藏该窗口,并没有释放,对于我这种js菜鸟来说,改那一坨代码要人 ...

  5. NSTimer、CADisplayLink 内存泄漏

    NSTimer.CADisplayLink 内存泄漏 内存泄漏的原因 CADisplayLink 要用 Taget 和 Selector 初始化,NSTimer 也可以用类似的方法初始化.这样初始化之 ...

  6. PHP垃圾回收机制理解

    使用的是"引用计数"方式进行回收.简单地理解的话,就是每个分配的内存区域都有一个计数器,记录有多少个变量指针指向这片内存.当指向该片内存的指针数量为0,那么该片内存区域就可以被回收 ...

  7. Safe Area Layout Guide

    原文:Safe Area Layout Guide Apple在iOS 7中引入了topLayoutGuide和bottomLayoutGuide作为UIViewController属性.它们允许您创 ...

  8. JavaScript面向对象(三)——继承与闭包、JS实现继承的三种方式

      前  言 JRedu 在之前的两篇博客中,我们详细探讨了JavaScript OOP中的各种知识点(JS OOP基础与JS 中This指向详解 . 成员属性.静态属性.原型属性与JS原型链).今天 ...

  9. Nim or not Nim? hdu3032 SG值打表找规律

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  10. cocos2dx - v2.3.3编辑器骨骼动画

    接上一节内容:cocos2dx - v2.3.3编辑器简单使用及不同分辨率适配 本节主要Cocos骨骼动画的创建及使用 一.新建 用Cocos Studio工具新建一个状态栏项目.如下图: 当然也可以 ...