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目标检测的 ...
随机推荐
- Ubuntu相关IP配置(转)
配置文件:/etc/network/interfaces 打开后里面可设置DHCP或手动设置静态ip.前面auto eth0,让网卡开机自动挂载. 1. 以DHCP方式配置网卡 编辑文件/etc/ne ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- eclipse workspace 共享设置
总结一下,复制工作空间配置步骤如下: 1 使用eclipse新建workspace. 2 将新建的workspace下的.metadata\.plugins内容全部删除. 3 将需要拷贝的worksp ...
- C++: I/O流详解
一.输入流操作 1.read 无格式输入指定字节数 istream& read ( char* pch, int nCount ); istream& read ( unsigned ...
- SqlServer学习笔记【暂】
Sql学习笔记,暂时先保存在着,等不忙了再整理成章节,如果其中有问题的,还请各位大神不吝赐教! --------------------------------------所有的数据基于Northwi ...
- Django之博客系统邮件分享博客
在上一章中,我们创建了一个基础的博客应用,我们能在http://127.0.0.1:8000/blog/显示我们的博客.在这一章我们将尝试给博客系统添加一些高级的特性,比如通过email来分享帖子,添 ...
- Django会话,用户和注册之session
鉴于cookie的不安全,django自带的session框架会帮我们搞定这些问题 你可以用session 框架来存取每个访问者任意数据, 这些数据在服务器端存储,并对cookie的收发进行了抽象. ...
- D - Back and Forth(模拟)
Problem Statement Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis point ...
- Go语言资源教程:Redis介绍安装和使用
Redis的操作和使用 在安装好redis以后,我们这里给大家讲一下redis操作的一些知识. Redis支持的数据类型:string,hash,list,set,sorted set 我们来练习一下 ...
- C#中工厂模式的作用
1.比如,主要用于对扩展性有要求的功能. 以简单工厂为例: 接口Fun有三个实现 class FunA FunB FunC工厂 class Fac { public static Fun getF ...