015 3Sum 三个数的和为目标数字
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int target=0;
List<List<Integer>> res=new ArrayList<List<Integer>>();
int size=nums.length;
if(size<3||nums==null){
return res;
}
Arrays.sort(nums);
for(int i=0;i<size-2;++i){
if(i>0&&nums[i]==nums[i-1]){
continue;
}
int l=i+1;
int r=size-1;
while(l<r){
int sum=nums[i]+nums[l]+nums[r];
if(sum<target){
while(l<r&&nums[++l]==nums[l-1]);
}else if(sum>target){
while(l<r&&nums[--r]==nums[r+1]);
}else{
List<Integer> tmp=new ArrayList<Integer>();
tmp.add(nums[i]);
tmp.add(nums[l]);
tmp.add(nums[r]);
res.add(tmp);
while(l<r&&nums[++l]==nums[l-1]);
while(l<r&&nums[--r]==nums[r+1]);
}
}
}
return res;
}
}
015 3Sum 三个数的和为目标数字的更多相关文章
- 001 Two Sum 两个数的和为目标数字
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- 【LeetCode】15. 3Sum 三个数和为0
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- LeetCode第[16]题(Java):3Sum Closest (和目标值最接近的三个数的和)——Medium
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- 16 3Sum Closest(输出距离target最近的三个数的和Medium)
题目意思:给一个数组,给一个target,找三个数的和,这个和要与target距离最近,输出这个和 思路:这个题比3sum要稍微简单一点,如果需要优化,也可以去重,不过因为结果唯一,我没有去重. mi ...
- 15 3Sum(寻找三个数之和为指定数的集合Medium)
题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列 思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i.j.k ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- [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 ...
- No.015 3Sum
15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n i ...
- 第三十六节,目标检测之yolo源码解析
在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的 ...
随机推荐
- Luogu 3267 [JLOI2016/SHOI2016]侦察守卫
以后要记得复习鸭 BZOJ 4557 大佬的博客 状态十分好想,设$f_{x, i}$表示以覆盖完$x$为根的子树后还能向上覆盖$i$层的最小代价,$g_{x, i}$表示以$x$为根的子树下深度为$ ...
- boost::python的使用
boost::python库是pyhon和c++相互交互的框架,可以再python中调用c++的类和方法,也可以让c++调用python的类和方法 python自身提供了一个Python/C AP ...
- [学习笔记]scanf弊端以及解决方案
#include<stdio.h> #include<stdlib.h> #include<unistd.h> int main(void) { ]; //mems ...
- [译]Javascript中的mutators
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- JDBC 配置环境
一.配置JDBC环境:先下载驱动jar包 1.配置classpath环境变量 如(E:\jdbc\mysql-connector-java-5.1.7-bin.jar) 2.数据库URL: 2.1 ...
- Python lib库docker-py和docker的区别
1)两者的安装方式 pip install docker A Python library for the Docker Engine API pip install docker-py A Pyth ...
- ECMAScript 6 一些有意思的特性
主要参考了下面两篇博文,对ES6的新特性做一些笔记,加深印象 ES6新特性概览 - 刘哇勇 - 博客园 es6快速入门 - _marven - 博客园 *号函数 迭代函数生成器 我能想到的生成器使用场 ...
- [51nod1222] 最小公倍数计数(莫比乌斯反演)
题面 传送门 题解 我此生可能注定要和反演过不去了--死都看不出来为啥它会突然繁衍反演起来啊-- 设\(f(n)=\sum_{i=1}^n\sum_{j=1}^n[{ij\over\gcd(i,j)} ...
- #6145. 「2017 山东三轮集训 Day7」Easy 动态点分治
\(\color{#0066ff}{题目描述}\) JOHNKRAM 最近在参加 C_SUNSHINE 举办的聚会. C 国一共有 n 座城市,这些城市由 n−1 条无向道路连接.任意两座城市之间有且 ...
- P1472 奶牛家谱 Cow Pedigrees
题意:问你指定二叉树有几种 1.高度为k 2.节点数为n 3.每个点的度为0或2 爆搜------->30分QAQ 首先,因为每个节点度为0或2, 所以如果n是偶数直接输出0就行了吧(嘿嘿) 如 ...