-
Notifications
You must be signed in to change notification settings - Fork 4
/
4sum.js
52 lines (50 loc) · 1.2 KB
/
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
var DEBUG = process.env.DEBUG;
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
nums = nums.sort(function (a, b) {
return a - b;
});
var ans = [];
for (var i = 0; i < nums.length;) {
for (var l = i + 1; l < nums.length;) {
var fixed = nums[i] + nums[l];
var j = l + 1; var k = nums.length - 1;
while (j < nums.length && k >= 0 && j < k) {
var curr = fixed + nums[j] + nums[k];
if (curr === target) {
ans.push([nums[i], nums[l], nums[j], nums[k]]);
do {
j++;
} while (j < nums.length && nums[j-1] === nums[j])
do {
k--;
} while (k >= 0 && nums[k+1] === nums[k])
} else if (curr < target) {
j++;
} else {
k--;
}
}
do {
l++;
} while (l < nums.length && nums[l] == nums[l-1]);
}
do {
i++;
} while (i < nums.length && nums[i] == nums[i-1]);
}
return ans;
};
if (DEBUG) {
[
[[], 0],
[[1, 0, -1, 0, -2, 2], 0],
[[1, 0, -1, 0, -2, 2, 2], 0]
].forEach(function (input) {
console.log(fourSum.apply(undefined, input));
});
}