[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 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三数之和的更多相关文章
- [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 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 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 ...
- 【LeetCode 15】三数之和
题目链接 [题解] 先把n个数字升序排个序. 然后枚举三元组最左边的那个数字是第i个数字. 之后用两个指针l,r移动来获取三元组的第2个和第3个数字. (初始值,l=i+1,r = n-1); 如果a ...
- [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 ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- LeetCode 第15题-三数之和
1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...
- LeeCode数组第15题三数之和
题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- 【LeetCode每天一题】3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
随机推荐
- Xamarin SearchView 用法摘记
与Windows开发不同,这个控件的事件比较难找,费了半天劲才知道应该用哪个事件.核心代码如下: public class MainActivity : Activity { protected ov ...
- session token两种登陆方式
Session 和 Token 其实Session和Token总体上还是很相似的,但是也有以下区别: 1. 过期时间:Session的过期时间存在cookie的Max-age字段,Token的过期时间 ...
- IO流小笔记
File file=new File ();括号里面写路径 exists()判断文件是否存在:isfile()是判断已经存在的文件是文件还是目录: mkdir()和createNewFile()区别在 ...
- c# winform窗体如何设置才可以不能随意拖动大小
执行以下两个步骤,能够禁止用户改变窗体的大小 (一)步骤1 设置窗体的FormBorderStyle属性为下列五个值中的任意一个 None:将窗口设置为无边框.无标题栏.用户无法改变窗口的大小,也无法 ...
- Centos7安装mysql5.6.29shell脚本
创建脚本mysql.sh,直接运行sh mysql.sh #!/bin/bash if [ -d /software ] ;then cd /software else mkdir /software ...
- golang cache--go-cache
go-cache是一款类似于memached 的key/value 缓存软件.它比较适用于单机执行的应用程序. go-cache实质上就是拥有过期时间并且线程安全的map,可以被多个goroutine ...
- Hadoop与MPP是什么关系?有什么区别和联系?
HADOOP与MPP是什么关系?有什么区别和联系? 适用范围.应用领域分别是什么? 其实MPP架构的关系型数据库与Hadoop的理论基础是极其相似的,都是将运算分布到节点中独立运算后进行结果合并.个人 ...
- 查看mysql的版本号
查看mysql的版本号 1.1 在命令行登录mysql,即可看到mysql的版本号 [root@heyong ~]# mysql -uroot -p Enter password: Welcome t ...
- fastext 中文文本分类
1. 输入文本预处理, 通过jieba分词, 空格" "拼接文本串. 每行一个样本, 最后一个单词为双下划线表明label, __label__'xxx' . eg: 邱县 继 ...
- Pycharm2018的激活方法或破解方法(必须加host)
修改hosts文件将0.0.0.0 account.jetbrains.com添加到hosts文件最后,注意hosts文件无后缀,如果遇到无法修改或权限问题,可以采用覆盖的方法去替换hosts文件 修 ...