Given an array nums of n integers, are there elements abc in nums 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.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

题意:

给定一个数组和一个值target,找到所有三数加起来等于target的组合

Solution1:Two Pointers(left and right to meet each other)

1. Sort the array (coz we must check the duplicates)

2. Lock one pointer and do two sum with the other two

Step1: Sort the given array in ascending order

Step2: Lock pointer i, then do two sum with two pointers j, k

Step3:  checking nums[i] + nums[j] + nums[k] == target ?

if   nums[i] + nums[j] + nums[k] < target,  pointer j ++

if   nums[i] + nums[j] + nums[k] > target,  pointer k --

注意几点:

1、题目要求“The solution set must not contain duplicate triplets.” 每次移动 i , j , k 都注意查重

2、Arrays工具类中常用方法需要牢记:

Arrays.sort() 排序数组

Arrays.fill() 填充数组

Arrays.toString() 将int数组转成string数组

Arrays.asList() 将数组转成list集合

code:

 /*
Time Complexity: O(n^2). For each locked item, we need traverse the rest of array behind such item.
Space Complexity: O(1). We only used constant extra space.
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
int target = 0;
Arrays.sort(nums);
// corner case
if (nums.length < 3) return result; for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue; // skip duplicates
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
if (nums[i] + nums[j] + nums[k] < target) {
j++;
while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
} else if (nums[i] + nums[j] + nums[k] > target) {
k--;
while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
j++;
k--;
while (j < k && nums[j] == nums[j - 1]) j++; // skip duplicates
while (j < k && nums[k] == nums[k + 1]) k--; // skip duplicates
}
}
}
return result;
}
}


[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 三数之和

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

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

  4. 【LeetCode 15】三数之和

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

  5. [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 ...

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

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

  7. LeetCode 第15题-三数之和

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

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

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

  9. 【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. delphi 下载

    最新(更多)内容,请到  http://www.cnblogs.com/key-ok/p/3465486.html  Borland Pascal v7.1 (13.89 Mb)  Delphi 1 ...

  2. uniDAC的安装和使用

    1.解压后把UniDAC文件夹 2.在UniDAC\Source\Delphi7文件夹中找到Make.bat文件,鼠标右键“编辑”确认DELPHI7的安装路径是否正确(建议:设置成绝对路径了,防止因为 ...

  3. Cannot change version of project facet Dynamic Web Module to 2.4问题解决

    问题现象: eclipse中,有个maven web项目,报错:Cannot change version of project facet Dynamic Web Module to 2.4,截图如 ...

  4. Python 面向对象(三)

    继承的实现原理 Python支持多继承 多继承的时候  属性查找的顺序 研究经典类和新式类在属性查找的不同 主要是形成菱形关系才有深度跟广度 广度优先 Python的继承原理  Python3的内置方 ...

  5. Linux find 命令参数大全及示例

    Linux中find常见用法示例 命令格式:find path -option [-print] [ -exec -ok command] {} \; 参数说明: path:find命令所查找的目录路 ...

  6. JVM 符号引用与直接引用

       Java类从加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括,加载 ,验证 , 准备 , 解析 , 初始化 ,卸载 ,总共七个阶段.其中验证 ,准备 , 解析 统称为连接.     ...

  7. [持续交付实践] pipeline使用:项目样例

    项目说明 本文将以一个微服务项目的具体pipeline样例进行脚本编写说明.一条完整的pipeline交付流水线通常会包括代码获取.单元测试.静态检查.打包部署.接口层测试.UI层测试.性能专项测试( ...

  8. flask&nginx&gunicore部署

    部署流程: 1.处理服务器的基础环境, 安装和Python有关的软件 安装Python3-pip, Python3-dev apt install python3-pip apt install py ...

  9. crontab自动失效注意事项

    若用户密码过期,用户的crontab计划任务会自动失效.重设用户密码修改时间后,crontab会自动恢复. #使用chage -l 查看用户密码过期时间 chage -l user #使用chage ...

  10. python3 不知文件编码情况下打开文件代码记录

    import chardet path='test.txt' bytes = min(100, os.path.getsize(path)) raw = open(path, 'rb').read(b ...