[LC] 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]
] Time: O(N^2)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
res = []
if nums is None:
return res nums.sort()
for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
target = -nums[i]
left = i + 1
right = len(nums) - 1
while left < right:
if nums[left] + nums[right] == target:
lst = [nums[i], nums[left], nums[right]]
res.append(lst)
left += 1
right -= 1
while left < right and nums[left] == nums[left - 1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1
elif nums[left] + nums[right] < target:
left += 1
else:
right -= 1
return res
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
// skip i as well
if (i > 0 && nums[i - 1] == nums[i]) {
continue;
}
int j = i + 1, k = nums.length - 1;
// j and k cannot meet since may add the same number twice
while (j < k) {
int target = nums[i] + nums[j] + nums[k];
if (target == 0) {
res.add(Arrays.asList(nums[i], nums[j], nums[k]));
j += 1;
while (j < k && nums[j] == nums[j - 1]) {
j += 1;
}
} else if (target < 0) {
j += 1;
} else {
k -= 1;
}
}
}
return res;
}
}
[LC] 15. 3Sum的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 1. Two Sum&&15. 3Sum&&18. 4Sum
题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 15. 3Sum、16. 3Sum Closest和18. 4Sum
15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...
- 刷题15. 3Sum
一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...
- [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 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 15. 3Sum C++
参考资料: https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N ...
随机推荐
- Java 类提供了自定义的构造方法,那么类的默认构造不会被调用
以下代码无法通过编译: public class Test1 { public static void main(String[] args) { //int a=6; Foo obj=new Foo ...
- SQL语句、PL/SQL块和SQL*Plus命令之间的区别
SQL语句.PL/SQL块和SQL*Plus命令之间的区别 原文链接:https://blog.csdn.net/liuzhushiqiang/article/details/12320941 在 ...
- ES6 之 数值扩展
1.ES5 // Number类型重写了valueOf() toLocaleString() toString('进制')方法 let a = 10 console.log(a.valueOf()); ...
- bitcoin-cli
== Blockchain ==getbestblockhashgetblock "blockhash" ( verbosity ) getblockchaininfogetblo ...
- this 深度面试题3
window.val = 1; var obj = { val: 2, dbl: function () { this.val *= 2; val *= 2; console.log(val); co ...
- MySQL--数据导入
参考:http://blog.csdn.net/jyb2014/article/details/39294879?locationNum=13 可导入大文件. source 导入总是失败.
- 使用cropper插件裁剪并上传图片(Spring mvc)
cropper是一款使用简单且功能强大的图片剪裁jQuery插件.该图片剪裁插件支持图片放大缩小,支持图片旋转,支持触摸屏设备,支持canvas,并且支持跨浏览器使用. cropper有两种方式上传截 ...
- 计蒜客 引爆炸弹(DFS、并查集)
在一个 n×m 的方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地图上的所有炸弹,需要手动 ...
- 201403-1 相反数 Java
法1:排序后,首尾两个指针 法2:每个数的绝对值如果出现过,flag置为1,如果再次出现,就计数+1 本文采用法1 import java.util.Arrays; import java.util. ...
- -bash: fultter: command not found
flutter build apk bash: flutter: command not found 在studio中的控制台出现上面错误(如图所示) 解决办法: 安装flutter时,安装时可以执行 ...