-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4Sum.js
61 lines (54 loc) · 1.72 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
53
54
55
56
57
58
59
60
61
/**
* https://leetcode.com/problems/4sum/description/
* Difficulty:Medium
*
* Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
* Note: The solution set must not contain duplicate quadruplets.
* For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
* A solution set is:
* [
* [-1, 0, 0, 1],
* [-2, -1, 1, 2],
* [-2, 0, 0, 2]
]
*/
/**
* 解题思路
* 跟三个数想加类似,但是每次遍历都有过滤和之前重复的元素
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[][]}
*/
var fourSum = function(nums, target) {
if (nums.length < 4) return []
let result = [], low, height, current, len = nums.length
nums.sort(function(a, b) { return a-b })
for(var i = 0; i < len; i++) {
// 如果相同直接跳过,防止重复
if (i != 0 && nums[i] == nums[i-1]) continue
for(var j = i + 1; j < len; j++) {
// 如果相同直接跳过,防止重复
if (j != i+1 && nums[j] == nums[j-1]) continue
current = nums[i] + nums[j];
low = j + 1;
height = nums.length - 1
while(low < height) {
if (nums[low] + nums[height] + current == target) {
result.push([nums[i], nums[j], nums[low], nums[height]])
do {
// 如果相同直接跳过,防止重复
low++;
} while(nums[low] == nums[low - 1])
} else if (nums[low] + nums[height] + current > target) {
height--
} else if (nums[low] + nums[height] + current < target) {
low++
}
}
}
}
return result
};
console.log(fourSum([-1,0,1,2,-1,-4], -1))