-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.四数之和.js
44 lines (43 loc) · 1.15 KB
/
18.四数之和.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
/*
* @lc app=leetcode.cn id=18 lang=javascript
*
* [18] 四数之和
*/
// @lc code=start
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function (nums, target) {
nums = nums.sort((a, b) => a - b);
if (nums.length < 4) return [];
let result = [];
for (let i = 0; i < nums.length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
for (let j = i + 1; j < nums.length - 2; j++) {
if (j - i > 1 && nums[j] == nums[j - 1]) continue;
let start = j + 1;
let end = nums.length - 1;
while (start < end) {
const diff = target - (nums[i] + nums[j]);
if (nums[start] + nums[end] > diff) {
end--;
} else if (nums[start] + nums[end] < diff) {
start++;
} else {
if (end === nums.length - 1 || nums[start] !== nums[start - 1] || nums[end] !== nums[end + 1]) {
result.push([nums[i], nums[j], nums[start], nums[end]]);
}
start++;
end--;
}
}
}
}
// console.log(result);
return result;
};
fourSum([0, 0, 0, 0], 0);
fourSum([-2, -1, -1, 1, 1, 2, 2], 0);
// @lc code=end