Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
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]
]
详见:https://leetcode.com/problems/3sum/description/
实现语言:Java
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 三个数的和为目标数字的更多相关文章

  1. 001 Two Sum 两个数的和为目标数字

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. 【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 ...

  3. 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 ...

  4. 16 3Sum Closest(输出距离target最近的三个数的和Medium)

    题目意思:给一个数组,给一个target,找三个数的和,这个和要与target距离最近,输出这个和 思路:这个题比3sum要稍微简单一点,如果需要优化,也可以去重,不过因为结果唯一,我没有去重. mi ...

  5. 15 3Sum(寻找三个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列 思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i.j.k ...

  6. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  7. [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 ...

  8. No.015 3Sum

    15. 3Sum Total Accepted: 131800 Total Submissions: 675028 Difficulty: Medium Given an array S of n i ...

  9. 第三十六节,目标检测之yolo源码解析

    在一个月前,我就已经介绍了yolo目标检测的原理,后来也把tensorflow实现代码仔细看了一遍.但是由于这个暑假事情比较大,就一直搁浅了下来,趁今天有时间,就把源码解析一下.关于yolo目标检测的 ...

随机推荐

  1. HDU 2102 A计划 (BFS或DFS)

    题意:中文题. 析:是一个简单的搜索,BFS 和 DFS都可行, 主要是这个题有一个坑点,那就是如果有一层是#,另一个层是#或者*,都是过不去的,就可以直接跳过, 剩下的就是一个简单的搜索,只不过是两 ...

  2. matlab处理:批处理图像分块

    有一个图像分块的代码,可以直接将一幅图像分为5*5的小块,代码如下: %[FileName,PathName] = uigetfile('*.*','Select the image'); Im=im ...

  3. 【C#】 创建、部署和调用WebService的简单示例

    C# 创建.部署和调用WebService的简单示例 <div class="postBody"> <div id="cnblogs_post_body ...

  4. nodejs nodejs的操作

    nodejs的操作 由于版本造成的命令不能正常安装,请参考五问题 一.概念: 参考百度百科: http://baike.baidu.com/link?url=aUrGlI8Sf20M_YGk8mh-- ...

  5. linux通配符与基础正则

    1.特殊符号: ' '     单引号          单引号的内容     写什么就是什么,不会被当成特殊字符.       单引号实例:      [root@oldboyedu-sh01 ~] ...

  6. Iterator 遍历器

    1.遍历器(Iterator)是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). 2.Iterator ...

  7. JBOSS AS 性能调整优化

    1 减少日志的输出量 *\jboss-4.2.3.GA\server\default\conf\jboss-log4j.xml 根据不同的日志级别:一共有5个等级,越往下输出的东西越详细.一般没什么特 ...

  8. 新姿势!Redis中调用Lua脚本以实现原子性操作

    背景:有一服务提供者Leader,有多个消息订阅者Workers.Leader是一个排队程序,维护了一个用户队列,当某个资源空闲下来并被分配至队列中的用户时,Leader会向订阅者推送消息(消息带有唯 ...

  9. 16.Shortest Unsorted Continuous Subarray(最短未排序子数组)

    Level:   Easy 题目描述: Given an integer array, you need to find one continuous subarray that if you onl ...

  10. pytorch 迁移学习[摘自官网]

    迁移学习包含两种:微调和特征提取器. 微调:对整个网络进行训练,更新所有参数 特征提取器:只对最后的输出层训练,其他层的权重保持不变 当然,二者的共性就是需要加载训练好的权重,比如在ImageNet上 ...