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目标检测的 ...
随机推荐
- 11.树形Model/View实例
任务1:显示如图的树形结构 思考: 1.使用QTreeView显示. 2.Model使用QStandardItemModel,qt的一个标准model. 3.QStandardItemModel下每一 ...
- VSTO的简单用法
一直听说vsto这个名词,还真不知道什么意思,今天了解了一下,原来他的全程是Visual Studio Tools For Office,说他是VBA的替身(VBA俺也不是很懂),刚才上网查询做了个例 ...
- 并没有看起来那么简单leetcode Generate Parentheses
问题解法参考 它给出了这个问题的探讨. 超时的代码: 这个当n等于7时,已经要很长时间出结果了.这个算法的复杂度是O(n^2). #include<iostream> #include&l ...
- [转]ORA-12516, TNS:listener could not find available handler with matching protocol stack
转至:http://blog.csdn.net/MichaelSrc/article/details/6760247 1.查看当前连接进程数 SQL>select count(*) from v ...
- C#模拟进度条
自己看源码 using System; namespace ConsoleTest { class Program { static void Main(string[] args) { Consol ...
- Dapper相关了解
公司新项目用的是Dapper,做的时候没有具体看dapper的具体用法,现在回来回顾总结一下. 1-总体介绍dapper 我们都知道ORM全称叫做Object Relationship Mapper, ...
- 完整读写txt 并提取{}里的内容
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...
- unit vs单元测试
vs单元测试(unit) 一.什么是单元测试及它的作用? 在小量代码编写时,往往可以通过新建控制台项目(Console Application),新建网站项目(Web Form)等,在其中敲入测试代码 ...
- C 语言 clock() 函数,例:计算多项式值
C 语言 clock() 函数,例:计算多项式值 /** * clock(): 捕捉从程序开始运行到 clock() 被调用时所耗费的时间. * 这个时间单位是 clock tick, 即" ...
- UIStepper更加详细的图文理解
前言 UIStepper是一个微调器,该控件的外观和UISwitch相似,但该控件上包含了+,-两个按钮,共同用于控制某个值的增.减. 它继承了UIControl基类,默认属于活动控件,它可以与用户交 ...