LeetCode--015--三元之和(java)
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
] i做主线的遍历,从头至尾寻找满足条件的其他两个数字。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0;i < nums.length-2;i++){
if(i > 0 && nums[i] == nums[i - 1])continue;//一定要加这一句,否则会重复,所给例子会输出两个[-1,1,0]
int low = i + 1,high = nums.length-1,sum = 0 - nums[i];
while(low < high){
if(nums[low] + nums[high] == sum){
res.add(Arrays.asList(nums[i],nums[low],nums[high]));
while(low < high && nums[low] == nums[low + 1]) low++;
while(low < high && nums[high] == nums[high - 1]) high--;
low ++;
high--;
}else if(nums[low] + nums[high] < sum){
low ++;
}else high--;
}
}
return res;
}
}
2019-04-14 09:49:33
LeetCode--015--三元之和(java)的更多相关文章
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode两数之和
LeetCode 两数之和 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode 三数之和 — 优化解法
LeetCode 三数之和 - 改进解法 题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复 ...
- 【数据结构】Hash表简介及leetcode两数之和python实现
文章目录 Hash表简介 基本思想 建立步骤 问题 Hash表实现 Hash函数构造 冲突处理方法 leetcode两数之和python实现 题目描述 基于Hash思想的实现 Hash表简介 基本思想 ...
- 1. 两数之和【Leetcode中国,by java】
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- 【JAVA、C++】LeetCode 015 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 ...
随机推荐
- mysql脚本手动修改成oracle脚本
今天有一个需求,立了一个新项目,新项目初步定了使用了现有的框架,但数据库要求由原来的mysql改成oracle,所以原来的基础版本的数据库脚本就需要修改成符合oracle的脚本,修改完成后,总结了一下 ...
- excel将百分比数据转为数值格式
由于于原给定的数据是百分比格式的, 所以先在excel中将数据格式改为数值 修改步骤: 单纯更改单元格格式为数值没用,先在空白单元格输入数值格式的1,复制该数字,选中要转换格式的数据, 右键 ---- ...
- C++——创建类的时候用new与不用new 的区别(转)
C++在创建对象的时候可以采用两种方式:(例如类名为Test) Test test 或者 Test* pTest = new Test(). 这两种方法都可以实例化一个对象,但是这两种 ...
- docker单机网络类型
docker单机网络类型概述 Docker 安装时会自动在 host 上创建三种网络 分别为 bridge host none . 可用 docker network ls 命令查看 ...
- Hive为什么要启用Metastore?
转载来自: https://blog.csdn.net/qq_40990732/article/details/80914873 https://blog.csdn.net/tp15868352616 ...
- C#中MemoryStream类的介绍
MemoryStream位于System.IO命名空间,为系统内存提供流式的读写操作.常作为其他流数据交换时的中间对象操作. 1.MemoryStream类封装一个字节数组,在构造实例时可以使用一个字 ...
- Docker 推送镜像到hub.docker
1.Docker镜像文件:lails.server.demo:1.0, 2.登录Docker:docker login[根据提示输入用户名/密码] 3.执行:docker push lails.ser ...
- 2014西安赛区C题
将A[i]同他后面比他小的建边,然后求最大密度子图 #include <iostream> #include <algorithm> #include <string.h ...
- codeforces 985B Switches and Lamps
题意: 有n个开关,m盏灯. 一个开关可以控制多个灯,一旦一个灯开了之后,之后再对这个灯的操作就没用了. 问是否存在一个开关,去掉了这个开关之后,按下其它开关之后所有的灯还是亮的. 思路: 首先统计每 ...
- maven ${path.separator}
${path.separator} 兼容 windows & linux: <compilerArguments> <verbose /> <bootclassp ...