-
Notifications
You must be signed in to change notification settings - Fork 0
/
4sum.js
36 lines (36 loc) · 905 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
34
35
36
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
const len = nums.length;
if (len < 4) return [];
nums.sort((a, b) => a - b);
const res = [];
for (let i = 0; i < len - 3; i++) {
// 去重i
if (i > 0 && nums[i] === nums[i - 1]) continue;
for (let j = i + 1; j < len - 2; j++) {
// 去重j
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
let l = j + 1,
r = len - 1;
while (l < r) {
const sum = nums[i] + nums[j] + nums[l] + nums[r];
if (sum < target) {
l++;
continue;
}
if (sum > target) {
r--;
continue;
}
res.push([nums[i], nums[j], nums[l], nums[r]]);
while (l < r && nums[l] === nums[++l]);
while (l < r && nums[r] === nums[--r]);
}
}
}
return res;
};