-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.四数之和.js
58 lines (55 loc) · 1.6 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
52
53
54
55
56
57
58
/*
* @lc app=leetcode.cn id=18 lang=javascript
*
* [18] 四数之和
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
const numList = nums.sort((a, b) => a - b);
const results = [];
const findNum = (nums, target, N, tempList, result) => {
const len = nums.length;
if (len < N || N < 2) return;
if (N === 2) {
l = 0;
r = len - 1;
while (l < r) {
if (nums[l] + nums[r] === target) {
result = result.concat([nums[l], nums[r]]);
while (l < r && nums[l] === nums[l - 1]) {
l += 1;
}
while (r > l && nums[r] === nums[r + 1]) {
r -= 1;
}
} else if (nums[l] + nums[r] < target) {
l += 1;
} else {
r -= 1;
}
}
} else {
for (let i = 0; i < len; i++) {
if (i === 0 || (i > 0 && nums[i - 1] != nums[i])) {
const insertList = nums.slice(i + 1, len + 1);
findNum(
insertList,
target - nums[i],
N - 1,
[...tempList, nums[i]],
result
);
}
}
}
};
findNum(numList, target, 4, [], results);
console.log(results);
return results;
};
// @lc code=end