-
Notifications
You must be signed in to change notification settings - Fork 0
/
4sum.js
33 lines (31 loc) · 973 Bytes
/
4sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
let res = [];
nums.sort((a,b)=>(a-b));
for(i=0;i<nums.length;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
for(j=i+1;j<nums.length;j++){
if(j>i+1 && nums[j]==nums[j-1]) continue;
let l = j+1, r = nums.length-1;
while(l<r){
if(nums[i]+nums[j]+nums[l]+nums[r]===target){
res.push([nums[i],nums[j],nums[l],nums[r]])
while(l<r && nums[l]===nums[l+1]) l++;
while(l<r && nums[r]===nums[r-1]) r--;
r--;
l++;
}else if(nums[i]+nums[j]+nums[l]+nums[r]<target){
l++;
}else{
r--;
}
}
}
}
return res;
};
fourSum([4,-9,-2,-2,-7,9,9,5,10,-10,4,5,2,-4,-2,4,-9,5],-13);