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. geyear和getfullyear的区别

    getYear(),IE中获得正确年份,但有的浏览器获取的是当前年份-1900的值.而IE是当大于2000时,直接加上1900 getFullYear(),都是可以获得正确年份. 所以建议使用getF ...

  2. php单例连接数据库

    mysql_connect() 后续的php就不支持了,所以会报错. 现在改为使用mysqli_connect(),需要开启php扩展哟! <?php /** * 设计模式之单例模式 * $_i ...

  3. java实现oracle数据库基本操作

    import java.sql.*; import java.util.ArrayList; import java.util.List; //使用jdbc连接 public class TestOr ...

  4. docker应用笔记

    first install it: 首先安装: apt install docker.io 基本概念: 镜像:相当于虚拟机里的磁盘文件,里面有一套配置好的系统,应用程序 容器:相当于一个虚拟机实例,一 ...

  5. 虚拟WEB目录的映射原理

    一个文件系统目录可以被映射成为多个虚拟WEB目录,虚拟WEB目录名称可以是多级目录结构的形式,tomca按照最长路径匹配原则处理请求的URL 设置WEB站点的根目录: <Host>元素的a ...

  6. gephi安装后无法打开

    具体解决的方法是找到gephi.conf文件(在“gephi安装目录\etc”中)文件,添加下面的一行,指定jdkhome的路径. jdkhome="C:\Program Files (x8 ...

  7. DelayQueue使用示例之KTV包厢记时

    在学习Java 多线程并发开发过程中,了解到DelayQueue类的主要作用:是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走. ...

  8. STM32获取DHT11温度传感器数据

    准备物件 STM32F103C8T6核心板 ST-LINK V2 DHT11 杜邦线若干 连接线 STM32F103C8T6芯片管脚图 管脚说明 连接仿真器 STM32 ST-LINKV2 VCC V ...

  9. Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)

    当任何时候觉你得难受了,其实你的大脑是在进化,当任何时候你觉得轻松,其实都在使用以前的坏习惯. 通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车 ...

  10. 调试 ASP.NET Core 2.0 源代码

    在Visual Studio 2017中可以通过符号以及源链接,非常方便对 ASP.NET Core 2.0中源代码进行调试.在这篇文章中,我们将重点介绍如何使用源链接对ASP.NET Core源进行 ...