给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
nums=nums.sort(function(a,b){return a-b});//先排序
var arr=[];
for(i=0;i<nums.length-3;i++){//第一个
if (i>0 && nums[i-1]==nums[i]) continue
for(j=i+1;j<nums.length-2;j++){//第二个
if (j>i+1 && nums[j-1]==nums[j]) continue
var k=nums.length-1
var c=j+1;
while(c<nums.length-1&&c!=k){//第三个
var sum=nums[i]+nums[j]+nums[c]+nums[k]
if (c>j+1 &&nums[c]== nums[c-1]){
c++;
continue;
}
if (k<nums.length-1 &&nums[k]==nums[k+1]){
k--;
continue;
}
if(sum==target){
arr.push([nums[i],nums[j],nums[c],nums[k]]);
c++
k=nums.length-1
}
else if(sum<target){
c++
}
else{
k--
}
}
}
}
return arr
};

LeetCode18.四数之和 JavaScript的更多相关文章

  1. LeetCode18. 四数之和

    LeetCode18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值 ...

  2. Leetcode12. 整数转罗马数字Leetcode18. 四数之和

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

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

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

  4. [Swift]LeetCode18. 四数之和 | 4Sum

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  5. leetcode18 四数之和 双指针

    题外话: 这道题让我想起了 挑战程序设计竞赛有一个抽签问题,类似的a+b+c+d=target,可以重复使用一个数. a+b+c+d=target转化为 a+b=target-c-d.  如果只是判断 ...

  6. LeetCode第十八题-四数之和

    4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2] ...

  7. ACM_四数之和

    四数之和 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个不同的整数,判断能否从中选4次,4个数和刚好为m.数字可重复选取. ...

  8. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  9. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

随机推荐

  1. JavaScript this指向相关内容

    1,默认绑定this指向windw对象 看代码: function test(C){ var a = 123 function b(){}; } 在预编译环节当中. OA{ arguments:[1] ...

  2. 各种常用的JSON接口

    这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用JSONP调用. 其中一些接口提供用例参照:http://www.bejson.com/webInterface.php 天气接口 气象局 ...

  3. Java 监听器,国际化

    1. 监听器 1.1 概述 监听器: 主要是用来监听特定对象的创建或销毁.属性的变化的! 是一个实现特定接口的普通java类! 对象: 自己创建自己用 (不用监听) 别人创建自己用 (需要监听) Se ...

  4. Threading in C# 5

    Part 5: Parallel Programming In this section, we cover the multithreading APIs new to Framework 4.0 ...

  5. [使用教程]cocostudio之UI编辑器动画模式

    有坑! 1. 使用 (1)点击动画模式按钮,进入动画模式 (2)[关键1]左下角动作列表,右键添加动画 (3)[关键2]渲染区选择要动画的控件,右键编辑动画 * 可以看到最下面多了关键帧 (4)在右边 ...

  6. C# 操作Excel 格式

    数字(Range.NumberFormatlocal 属性)常规:Range.NumberFormatlocal = "G/通用格式"数值:Range.NumberFormatlo ...

  7. zookeeper 的监控工具

    zookeeper 的监控工具         公司很多产品会使用zookeeper,比如Meta消息中间件,在测试的过程中,我们经常需要查询zookeeper里面的信息来精确定位问题.目前项目中有开 ...

  8. 启动入口Start.java分析

    框架的启动器在包:org.ofbiz.base.start 入口为:Start.java的main方法 Start.java启动器内容: 步骤 详情 入参校验 help/status/shutdown ...

  9. Oracle练习详解

    --1.查询emp表,显示薪水大于2000,且工作类别是MANAGER的雇员信息 select * from emp where sal > 2000and job = 'MANAGER'; - ...

  10. mysql 修改已存在的表增加ID属性为auto_increment自动增长

    今天有需要将已经存在表设置自动增长属性 具体如下 alter table customers change id id int not null auto_increment primary key; ...