题目:

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.
    For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

    A solution set is:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

思路:

定义一对指针,指向两头。再定义一对指针,指向中间的两个元素。加起来看看跟target比较一下。决定内部指针怎么移动。
注意结果可能出现重复的结果,需要做一下去重操作。
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
var n=nums.length,temp=0,res=[]; if(nums.length<4){
return [];
} nums.sort(function(a,b){return a-b;}); for(var i=0;i<n-3;i++){
for(var j=n-1;j>i+2;j--){
temp=target-nums[i]-nums[j];
var a=i+1,
b=j-1;
while(a<b){
var sum=nums[a]+nums[b];
if(sum==temp){
var arr=[nums[i],nums[a],nums[b],nums[j]];
res[res.length]=arr;
a++;
b--;
}else if(sum<temp){
a++;
}else{
b--;
}
}
}
} return res.unique();
}; Array.prototype.unique = function(){
var res = [];
var json = {};
for(var i = 0; i < this.length; i++){
if(!json[this[i]]){
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
}

【数组】4Sum的更多相关文章

  1. 18. 4Sum -- 找到数组中和为target的4个数

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  2. [LeetCode] 4Sum II 四数之和之二

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

  3. LeetCode:3Sum, 3Sum Closest, 4Sum

    3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...

  4. No.018:4Sum

    问题: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  5. 2sum、3sum、4sum以及任意连续的数的和为sum、任意连续或者不连续的数的和为sum

    2sum 如果数组是无序的,先排序(n*logn),然后用两个指针i,j,各自指向数组的首尾两端,令i=0,j=n-1,然后i++,j--,逐次判断a[i]+a[j]?=sum,如果某一刻a[i]+a ...

  6. 2Sum,3Sum,4Sum,kSum,3Sum Closest系列

    1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...

  7. 在数组中找几个数的和等于某个数[LeetCode]

    首先明确一点,这个方面的问题设计到的知识点是数组的查找的问题.对于类似的这样的查找操作的具体办法就是三种解决方法: 1.暴力算法,多个for循环,很高的时间复杂度 2.先排序,然后左右夹逼,但是这样会 ...

  8. LeetCode数组解题模板

    一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...

  9. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

随机推荐

  1. web.xml 404 500 配置

    web.xml <error-page> <error-code>404</error-code> <location>/error404.html&l ...

  2. Swift1.2与Xcode6.3 beta

    Xcode6.3和Swift1.2都已经发布.这次发布增强了Swift编译器也给Swift增加了一些新的特性.详细内容可以看这里.这里主要关注比较重要的内容. 编译器的改进 Swift1.2的编译器更 ...

  3. Java内存模型(二)

    volatile型变量的特殊规则 volatile是Java虚拟机提供的最轻量级的同步机制,当一个变量被定义成volatile后,它将具备两种特性,第一是保证此变量对所有线程的可见性,这里的“可见性” ...

  4. 删除map、list集合元素总结

    @Testpublic void removeElementFromMap(){Map<Integer, String> test = new HashMap<Integer, St ...

  5. TFS 如何強制撤銷被簽出的文件

    我们在使用TFS (Team Foundation Server) 源代码管理的时候,源代码管理会在每个PC上创建一个工作区,然后这个工作区域映像到服务器上的源码文件夹,我们在正常签入,签出的时候,我 ...

  6. 挂起的更改中的“解析”是什么意思?原来是微软错误的翻译

    [2017.4.5 补充] 收到微软TFS产品组的回复,由于版本分支丢失了本来已经修复的内容,并确认下一个版本将修复这个问题. 自从团队资源管理器的"挂起的更改中"可以链接相关工作 ...

  7. 开源项目之ASP.NET Core + Vue.js 的前后端分离的通用后台管理系统框架

    年前看了这个开源项目感觉很不错,这个小项目对于传统的.net 开发人员,想做技术提升是一个很不错的参考案例. 开源项目演示地址:https://dnczeus.codedefault.com/logi ...

  8. 如何在 .NET Core 上测试库

    设置解决方案. 可使用以下命令实现此目的: mkdir SolutionWithSrcAndTest cd SolutionWithSrcAndTest dotnet new sln dotnet n ...

  9. (C#)冒泡排序

    //冒泡排序 public static int[] Bubbling(int[] s) { int a; for (int i = 0; i < s.Length-1; i++) { for ...

  10. sqlServer数据库纵横表相互转化

    sqlServer  数据库纵横表相互转化 一.纵表转横表: 1.纵表: 2.横表: 3. 代码: select Name as '姓名', end) as '语文', end) as '数学', e ...