-
Notifications
You must be signed in to change notification settings - Fork 1
/
4sum.ts
68 lines (58 loc) · 1.72 KB
/
4sum.ts
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
62
63
64
65
66
67
68
// 方法一:
// 关键在于如何去重
export function fourSum (nums: number[], target: number): number[][] {
nums.sort((a, b) => a - b)
let result = []
for (let i = 0, len = nums.length - 3; i < len; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue // 去重
for (let j = i + 1, len = nums.length - 2; j < len; j++) {
if (j > i + 1 && nums[j] === nums[j - 1]) continue // 去重
let left = j + 1
let right = nums.length - 1
while (left < right) {
if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
right--
} else if (nums[i] + nums[j] + nums[left] + nums[right] < target) {
left++
} else {
result.push([nums[i], nums[j], nums[left], nums[right]])
while (left < right && nums[right] === nums[right - 1]) { // 去重
right--
}
while (left < right && nums[left] === nums[left + 1]) { // 去重
left++
}
right--
left++
}
}
}
}
return result
}
// 方法二:
// 回溯法
// 记录重复元素,不成参与判断
export function fourSum2 (nums: number[], target: number): number[][] {
const result: number[][] = []
const path: number[] = []
nums.sort((a, b) => a - b)
const backtracking = (start: number) => {
if (path.length === 4) {
if (path[0] + path[1] + path[2] + path[3] === target) {
result.push([...path])
}
return
}
let j = null // 去除当前层级重复元素
for (let i = start; i < nums.length; i++) {
if (j === nums[i]) continue
j = nums[i]
path.push(nums[i])
backtracking(i + 1)
path.pop()
}
}
backtracking(0)
return result
}