-
Notifications
You must be signed in to change notification settings - Fork 279
/
18. 4Sum.js
41 lines (41 loc) · 1.32 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
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
var results = [];
nums.sort(function(a,b) { return a - b });
for(var i = 0; i < nums.length - 3; i++) {
while(i > 0 && i < nums.length - 3 && nums[i] === nums[i - 1]) {
i++;
}
for(var j = i + 1; j < nums.length - 2; j++) {
while(j > i + 1 && j < nums.length - 2 && nums[j] === nums[j - 1]) {
j++;
}
var low = j + 1;
var high = nums.length - 1;
var newTarget = target - (nums[i] + nums[j]);
while(low < high) {
partialSum = nums[low] + nums[high];
if(partialSum === newTarget) {
results.push([nums[i], nums[j], nums[low], nums[high]]);
high--;
low++;
while(low < high && nums[low] === nums[low - 1]) {
low++;
}
while(low < high && nums[high] === nums[high + 1]) {
high--;
}
} else if (partialSum > newTarget) {
high--;
} else {
low++;
}
}
}
}
return results;
};