【数组】4Sum
题目:
Given an array S of n integers, are there elements a, b, c, 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)
思路:
/**
* @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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 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 = ...
- 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 ...
- 2Sum,3Sum,4Sum,kSum,3Sum Closest系列
1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于 ...
- 在数组中找几个数的和等于某个数[LeetCode]
首先明确一点,这个方面的问题设计到的知识点是数组的查找的问题.对于类似的这样的查找操作的具体办法就是三种解决方法: 1.暴力算法,多个for循环,很高的时间复杂度 2.先排序,然后左右夹逼,但是这样会 ...
- LeetCode数组解题模板
一.模板以及题目分类 1.头尾指针向中间逼近 ; ; while (pos1<pos2) { //判断条件 //pos更改条件 if (nums[pos1]<nums[pos2]) pos ...
- 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 ...
随机推荐
- Get同步请求
//同步get请求 // NSURL: iOS 中的URL存储类,可存储网址或者文件路径 NSString *urlString = @"http://api.map. ...
- day06(Collection,List,ArrayList,LinkedList,iterator迭代器,增强for)
Collection 接口 方法实现 boolean add(Object o);//添加 boolean remove(Object o);//移除 修改方法 让实现类自己去实现修 ...
- (二分匹配 模板 KM)奔小康赚大钱--hdu--2255
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2255 代码: #include <iostream> #include <cstdio ...
- Eclipse的使用技巧
Eclipse有强大的编辑功能, 工欲善其事,必先利其器, 掌握Eclipse快捷键,可以大大提高工作效率. 小坦克我花了一整天时间, 精选了一些常用的快捷键操作,并且精心录制了动画, 让你一看就会. ...
- cxgrid动态显示行号
uses cxLookAndFeelPainters; type TMyCxGrid = class(TObject) class procedure DrawIndicatorCell( ...
- VCL消息处理机制
http://www.cnblogs.com/railgunman/archive/2010/12/10/1902524.html#2868236 说到VCL中的消息处理就不能不提到TApplicat ...
- Spring Boot 应用系列 1 -- Spring Boot 2 整合Spring Data JPA和Druid,双数据源
最近Team开始尝试使用Spring Boot + Spring Data JPA作为数据层的解决方案,在网上逛了几圈之后发现大家并不待见JPA,理由是(1)MyBatis简单直观够用,(2)以Hib ...
- rtmp官方标准规范详细解析
标准规范学习: rtmp消息结构,包括几个部分: 时戳:4 byte,单位毫秒.超过最大值后会翻转. 长度:消息负载的长度. 类型ID:Type Id 一部分ID范围用于rtmp的控制信令.还有一部 ...
- HTML/HTML5
HTML/HTML5 一.文档加载顺序. 文档入下,数字编号为加载顺序. <html><!--1--> <head><!--2--> <link ...
- Null类型的DateTime怎么用在TimeSpan上!
太TM简单了.. DateTime ts1 = Convert.ToDateTime(workinfo.WorkTime.ToString()); DateTime ts2 = Convert.ToD ...