-
Notifications
You must be signed in to change notification settings - Fork 0
/
18.四数之和.js
51 lines (49 loc) · 1.21 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
45
46
47
48
49
50
51
/*
* @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) {
let results = []
nums.sort((a, b) => a - b)
for (let i = 0; i < nums.length - 3; i++) {
while (i > 0 && i < nums.length - 3 && nums[i] === nums[i - 1]) {
i++
}
for (let j = i + 1; j < nums.length - 2; j++) {
while (j > i + 1 && j < nums.length - 2 && nums[j] === nums[j - 1]) {
j++
}
let low = j + 1
let high = nums.length - 1
let newTarget = target - (nums[i] + nums[j])
while (low < high) {
let 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
}
// @lc code=end
module.exports = { fourSum }