给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
] 答案参考:
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
    var result = new Array();
    var len = nums.length;
    var flag = 0;
    var hash = {};
    nums.sort((a, b) => {
        return a-b;
    });
    if(nums[0] > 0 || nums[len - 1] < 0) return result;
    for(var i = 0; i < len; i++){
        if(nums[i] === nums[i-1]) continue;
        flag = 0 - nums[i];
        var start = i + 1, end = len - 1;
        while(start < end){
            var middle = new Array();
            if(nums[start] + nums[end] < flag){
                start ++;
            } else if(nums[start] + nums[end] > flag){
                end--;
            } else {
                middle.push(nums[i]);
                middle.push(nums[start]);
                middle.push(nums[end]);
                if(!hash[middle]){
                    hash[middle] = true;
                    result.push(middle);
                }
                start += 1;
                end -= 1;
                while(start < end && nums[start] === nums[start - 1]){
                    start += 1;
                }
                while(start < end && nums[end] === nums[end + 1]){
                    end -= 1;
                }
            }
        }
    }
    return result; };

LeetCode15.三数之和 JavaScript的更多相关文章

  1. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  2. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  3. LeetCode15. 三数之和

    15. 三数之和 描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...

  4. LeetCode16.最接近的三数之和 JavaScript

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  5. [Swift]LeetCode15. 三数之和 | 3Sum

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  6. leetcode15 三数之和 双指针

    注意题目没要求数字只能用一次 a + b + c = 0 即为 -b=a+c,同时要求数字不全为正(然后发现a+b+c就行...不过多想想没坏处嘛) 先处理特殊情况,然后 先排序 注意不重复,只需要有 ...

  7. 南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和

    问题 A: 变位词 时间限制: 2 Sec  内存限制: 10 MB提交: 322  解决: 59提交 状态 算法问答 题目描述 请大家在做oj题之前,仔细阅读关于抄袭的说明http://www.bi ...

  8. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  9. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

随机推荐

  1. 【一】JMeter的介绍安装和使用

    利用JMeter进行性能测试 一.JMeter介绍二.Jmeter安装三.工作原理四.脚本录制五.运行JMeter进行测试六.JMeter主要组件介绍七.参数化设置八.动态数据关联九.使用插件进行服务 ...

  2. 分页存储过程ROW_NUMBER() over(order by pid desc)

    分页存储过程 : create proc usp_GetMyPhotos  @pageIndex int,   --当前页码  @pageSize int,   --每页多少条  @pageCount ...

  3. scss-@extend

    @extend指令用于共享规则和选择器之间的关系.它可以扩展所有其他类的样式在一个类中,也可应用于自己特定的样式. 查看如下scss@extend示例: .style{ font-size: 30px ...

  4. stateless函数里面的参数问题

    UI组件文件: function Selector({ status1, status2, status3 }){ return (<div><span>demo</sp ...

  5. git如何进行远程分支切换

    git如何进行远程分支切换 git上查看远程分支命令: git branch -a  例如: 然后我想切换到daily/1.0.0远程分支:前提是必须要创建一个本地分支,并让它和远程分支进行关联,gi ...

  6. Myeclipse中进行JUnit单元测试

    最近学习了在myeclipse中进行单元测试,写点东西总结总结. JUnit单元测试: 测试对象为一个类中的方法. juint不是javase中的部分,所以必须导入jar包,但是myeclipse自带 ...

  7. C++基础--结构体声名

    struct是一种数据结构,当需要存储的相关数据为一个集合时,struct是很好的选择;例如,当存储student,学生的学号, 名字,年龄,身高,就构成了一个集合,用stuct声名为: typede ...

  8. Latest SoC

    From: http://laoyaoba.com/ss6/html/68/n-505768.html 2014年国产ARM SoC芯片巡礼(上) 关注“集微网”,微信点播新闻.随要随有 来源: &l ...

  9. 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色

    简介 在简单恢复模式下,日志文件的作用仅仅是保证了SQL Server事务的ACID属性.并不承担具体的恢复数据的角色.正如”简单”这个词的字面意思一样,数据的备份和恢复仅仅是依赖于手动备份和恢复.在 ...

  10. Scope_Pre_Post

    @Scope , 设置bean的生命周期,示例:   @Scope(value="prototype")//设置生存范围,一般用 singleton或prototype 14.@P ...