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目标检测的 ...
随机推荐
- js的学习
对于 ff的 relatedTarget 及IE的toElement fromElement DOM通过event对象的relatedTarget属性提供了相关元素的信息.这个属性只对于mou ...
- Entity Framework Tutorial Basics(27):Update Entity Graph
Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...
- Mybatis避免出现语法错
在使用MyBatis的时候,可能会看起来没有问题,但是代码运行的时候出现意想不到的错误. 看如下代码: <update id="updateByPrimaryKeySelective& ...
- 带参宏定义和inline修饰的内联函数
带参宏定义和inline修饰的内联函数都是在编译时,用函数体替换掉宏调用或函数调用.这样用的好处是减少调用函数所花费的时间. 例如: 算法导论在讲到堆排序时说的,好的堆排序实现一般是把Left(i), ...
- winform改变控件的外形
GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0, 0, 40, 40); Region region = new Region(gp); c ...
- 使用cydia substrate 来进行android native hook
cydia不仅可以hook java代码,同样可以hook native代码,下面举一个例子来进行android native hook 我是在网上找到的supermathhook这个项目,在他基 ...
- 如何在Linux上使用x2go设置远程桌面
Until ACS supports Spice, if ever,you're better off with "on-VM" softare such RDP for Wind ...
- 用Oracle的函数,判断点是否在多边形内
转自:http://blog.csdn.net/familyshizhouna/article/details/68944683 参考:http://blog.csdn.net/qwlovedzm/a ...
- python内置函数print输出到文件,实现日志记录的功能
# bulid time 2018-6-22 import os import time def log(*args, **kwargs): # *kargs 为了通用 可不传 rule = &quo ...
- django2集成DjangoUeditor富文本编辑器
富文本编辑器,在web开发中可以说是不可缺少的.django并没有自带富文本编辑器,因此我们需要自己集成,在这里推荐大家使用DjangoUeditor,因为DjangoUeditor封装了我们需要的一 ...