forked from proYang/leetcode-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
018-4Sum.js
51 lines (45 loc) · 1.52 KB
/
018-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
/**
* https://leetcode.com/problems/4sum/description/
* Difficulty:Medium
*
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
* Note: The solution set must not contain duplicate quadruplets.
* For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
* A solution set is:
* [
* [-1, 0, 0, 1],
* [-2, -1, 1, 2],
* [-2, 0, 0, 2]
]
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
var len = nums.length;
if (len < 4) return [];
nums.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
// console.log(nums);
var ans = [];
for (var i = 0; i < len - 3; i++) {
for (var j = i + 1; j < len - 2; j++) {
var k = j + 1;
var l = len - 1;
while (k < l) {
var sum = nums[i] + nums[j] + nums[k] + nums[l];
if (sum === target) {
ans.push([nums[i], nums[j], nums[k], nums[l]]);
while (nums[l--] === nums[l] && nums[k++] === nums[k] && k < l);
}
else if (sum < target) while (nums[k++] === nums[k] && k < l);
else while (nums[l--] === nums[l] && k < l);
}
while (nums[j] === nums[j + 1]) j++;
}
while (nums[i] === nums[i + 1]) i++;
}
return ans;
};
console.log(fourSum([-5, -4, -2, -2, -2, -1, 0, 0, 1], -9));