[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 ...
随机推荐
- immutable.js使用总结
1. immutable相当于 JSON.parse 和 JSON.stringify: 2.引入redux中,除了 在最外层 reducer中 import { combineReducers } ...
- xamarin android 报错 Could not load assembly 'Xamarin.Android.Support.v7.AppCompat
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 Exception while loading assemblies: System.IO.FileNotFoundException: Cou ...
- cannot find package "context"
导入 github.com/go-sql-driver/mysql 和 database/sql 时,报cannot find package "context"的错误因为go1 ...
- 如何在idea中引入一个新maven项目
如何在idea中引入一个新的maven项目,请参见如下操作:
- 汉语言处理工具pyhanlp的简繁转换
繁简转换 HanLP几乎实现了所有我们需要的繁简转换方式,并且已经封装到了HanLP中,使得我们可以轻松的使用,而分词器中已经默认支持多种繁简格式或者混合.这里我们不再做过多描述. 说明: ·Ha ...
- NotePad++ 配置Python工作环境
下载地址:https://notepad-plus-plus.org/ Current Version: 7.5.3 sss 显示空格和指标符 为什么建议这么作?因为判断Python语句是否在同一层次 ...
- mysql中的锁
MYSQL不同的存储引擎支持不同的锁的机制 MyISAM 支持表锁,InnoDB支持表锁和行锁 表锁,行锁比较 表锁:开销小,加锁快:不会出现死锁:锁定力度大,发生锁冲突概率高,并发度最低 行锁:开销 ...
- 2017-07-06 eclipse在线安装SVN1.9插件
1,百度搜索subeclipse,点击第一个: 2,官网说,文档已移动到github wiki上: 3,打开github wiki,复制最新发布版本地址: 4,在eclipse里面,打开help-&g ...
- Linux操作redis 使用(VMwareWorkstation)
项目一般都部署到linux上面,记得刚出来的时候,第一家公司 服务器是windows系统,以后公司的项目都放在了linux上面,所以掌握linux的一些基本操作是一个程序员必备的知识,本次记录如何使用 ...
- Linux vim快捷键
1 替换 r 替换 先按r再按要替换的内容 2 按yy复制当前行 按p是粘贴 3 # add at 18-10-25 #-------------------------------- ...