-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.四数之和.ts
More file actions
93 lines (85 loc) · 1.95 KB
/
18.四数之和.ts
File metadata and controls
93 lines (85 loc) · 1.95 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* @lc app=leetcode.cn id=18 lang=typescript
*
* [18] 四数之和
*
* https://leetcode.cn/problems/4sum/description/
*
* algorithms
* Medium (38.37%)
* Likes: 1340
* Dislikes: 0
* Total Accepted: 360.2K
* Total Submissions: 938.8K
* Testcase Example: '[1,0,-1,0,-2,2]\n0'
*
* 给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a],
* nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
*
*
* 0 <= a, b, c, d < n
* a、b、c 和 d 互不相同
* nums[a] + nums[b] + nums[c] + nums[d] == target
*
*
* 你可以按 任意顺序 返回答案 。
*
*
*
* 示例 1:
*
*
* 输入:nums = [1,0,-1,0,-2,2], target = 0
* 输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
*
*
* 示例 2:
*
*
* 输入:nums = [2,2,2,2,2], target = 8
* 输出:[[2,2,2,2]]
*
*
*
*
* 提示:
*
*
* 1 <= nums.length <= 200
* -10^9 <= nums[i] <= 10^9
* -10^9 <= target <= 10^9
*
*
*/
export
// @lc code=start
function fourSum(nums: number[], target: number): number[][] {
nums.sort((a, b) => a - b)
const result: Array<[number, number, number, number]> = []
for (let a = 0; a < nums.length - 3; a++) {
if (a !== 0 && nums[a] === nums[a - 1]) {
continue
}
for (let b = a + 1; b < nums.length - 2; b++) {
if (b !== a + 1 && nums[b] === nums[b - 1]) {
continue
}
const sumAB = nums[a] + nums[b]
let d = nums.length - 1
for (let c = b + 1; c < nums.length - 1; c++) {
if (c !== b + 1 && nums[c] === nums[c - 1]) {
continue
}
while (c < d && sumAB + nums[c] + nums[d] > target) {
d--
}
if (c >= d) break
if (sumAB + nums[c] + nums[d] === target) {
result.push([nums[a], nums[b], nums[c], nums[d]])
}
}
}
}
return result
}
// @lc code=end