-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.四数之和.js
51 lines (44 loc) · 1.13 KB
/
18.四数之和.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
* https://leetcode-cn.com/problems/4sum/
*/
var fourSum = function(nums, target) {
if(nums.length < 4) return [];
const res = [];
// 排序
nums.sort((a,b) => a-b);
for(let i = 0; i < nums.length; i++) {
// 剪枝
if (nums[i] > target && nums[i] > 0) break;
// 去重
if (nums[i] === nums[i-1]) continue;
for (let j = i+1; j < nums.length; j++) {
if (nums[i] + nums[j] > target && nums[j] > 0) break;
if (nums[j] === nums[j-1] && j > i+1) continue;
let l = j+1, r = nums.length-1;
while (l < r) {
const sum = nums[i] + nums[j] + nums[l] + nums[r];
if (sum === target) {
res.push([nums[i], nums[j], nums[l], nums[r]]);
while (nums[l] === nums[l+1] && l < r) {
l++;
}
while (nums[r] === nums[r-1] && l < r) {
r--;
}
l++;
r--;
} else if (sum > target) {
r--;
} else {
l++;
}
}
}
}
console.log(res);
return res;
};
fourSum([-1,0,1,2,-1,-4], -1)