-
Notifications
You must be signed in to change notification settings - Fork 6
/
18. 4Sum.js
69 lines (59 loc) · 1.59 KB
/
18. 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
* 0 <= a, b, c, d < n
* a, b, c, and d are distinct.
* nums[a] + nums[b] + nums[c] + nums[d] == target
* You may return the answer in any order.
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var threeSum = function (nums, target) {
let ro = [];
for (let i = 0; i < nums.length - 2; i++) {
if (i === 0 || nums[i] > nums[i - 1]) {
const newTarget = target - nums[i];
let j = i + 1;
let k = nums.length - 1;
while (j < k) {
if (nums[j] + nums[k] === newTarget) {
ro.push([nums[i], nums[j], nums[k]]);
j++;
k--;
while (j < k && nums[j] === nums[j - 1]) {
j++;
}
while (j < k && nums[k] === nums[k + 1]) {
k--;
}
} else if (nums[j] + nums[k] < newTarget) {
j++;
} else {
k--;
}
}
}
}
return ro;
};
var fourSum = function (nums, target) {
let ro = [];
if (nums.length < 4) {
return ro;
}
nums = nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length - 3; i++) {
if (i === 0 || nums[i] > nums[i - 1]) {
const newTarget = target - nums[i];
const tempRo = threeSum(nums.slice(i + 1), newTarget);
for (let j = 0; j < tempRo.length; j++) {
ro.push([nums[i]].concat(...tempRo[j]));
}
}
}
return ro;
};
console.log(fourSum([1, 0, -1, 0, -2, 2], 0));
console.log(fourSum([2, 2, 2, 2, 2], 8));