-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4sum.js
51 lines (41 loc) · 1.41 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
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
if (nums.length < 4) return [];
var len = nums.length;
var res = [];
var l = 0;
var r = 0;
var sum = 0;
nums.sort((a, b) => (a - b));
for (var i = 0; i < len - 3; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
if (nums[i] + nums[len - 1] + nums[len - 2] + nums[len - 3] < target) continue;
for (var j = i + 1; j < len - 2; j++) {
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
if (nums[i] + nums[j] + nums[len - 1] + nums[len - 2] < target) continue;
l = j + 1;
r = len - 1;
while (l < r) {
sum = nums[i] + nums[j] + nums[l] + nums[r];
if (sum < target) {
l++;
} else if (sum > target) {
r--;
} else {
res.push([nums[i], nums[j], nums[l], nums[r]]);
while (l < r && nums[l] === nums[l + 1]) l++;
while (l < r && nums[r] === nums[r - 1]) r--;
l++;
r--;
}
}
}
}
return res;
};