[LeetCode] 15. 3Sum ☆☆☆(3数和为0)
描述
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]
]
解析
先排序,再确定一个数字,用后面的数据获取0 - num[i]的值,转成 2sum的问题。要注意跳过重复数据。
代码
解法1(解析所述)
public static List<List<Integer>> threeSum3(int[] num) {
if (num == null || num.length < 3) {
return new ArrayList<>();
}
Arrays.sort(num);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < num.length - 2; i++) {
if (i == 0 || (i > 0 && num[i] != num[i - 1])) {
int target = 0 - num[i];
int start = i + 1;
int end = num.length - 1;
while (start < end) {
if (num[start] + num[end] == target) {
res.add(Arrays.asList(num[i], num[start], num[end]));
//去除重复元素
while (start < end && num[start] == num[start + 1]) {
start++;
}
while (start < end && num[end] == num[end - 1]) {
end--;
}
start++;
end--;
} else if (num[start] + num[end] > target) {
end--;
} else {
start++;
}
}
}
}
return res;
}
数组中找等于指定数的集合
也可以用数组num,在其中找n个数等于指定数的解法。
[LeetCode] 15. 3Sum ☆☆☆(3数和为0)的更多相关文章
- [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三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- 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 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 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(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
随机推荐
- iOS 字符串和图片互转
for (UIImage *myImg in _imgArray) { NSData *imageData = UIImageJPEGRepresentation(myImg,0.5); NSStri ...
- html5调整音频音高
var audioSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3'; function buff ...
- Delphi中动态加载TreeView信息
unit Unit3; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Node.js使用ftp连接远程ftp服务器枚举和下载文件示例
示例代码: var Ftp = require('ftp'); var fs = require('fs'); var path = require('path'); // 首先判断参数中是否包含{d ...
- Python3之返回函数
参考:https://www.cnblogs.com/mzc1997/p/7641995.html Python中函数不仅可以作为参数还可以作为结果返回 >>> def pro1(c ...
- iOS自定义遮盖
@interface TGCover : UIView+ (id)cover;+ (id)coverWithTarget:(id)target action:(SEL)action; - (void) ...
- 如何去除PATH里的重复项并排序
注意sed的用法,linux和Mac os不同,linux是Gnu的,Mac是BSD的 PATH排序去掉重复内容 mac和linux的换行符替换方法不一样,如下是Mac下的操作 export PATH ...
- 最新 东方网java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.东方网等10家互联网公司的校招Offer,因为某些自身原因最终选择了东方网.6.7月主要是做系统复习.项目复盘.LeetCo ...
- 学习笔记:oracle学习一:oracle11g体系结构之体系结构概述和逻辑存储结构
目录 1.oracle 11g体系结构概述 1.1 三个重要概念 1.2 oracle数据库存储结构 2 逻辑存储结构 2.1 数据块(Data Blocks) 2.2 数据区(Extent) 2.3 ...
- Python 解LeetCode:606 Construct String from Binary Tree
题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...