Skip to content

Commit 55c359d

Browse files
committed
✨feat: add 面试题 17.19
1 parent 3b2b06b commit 55c359d

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

Index/数学.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@
7676
| [1738. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/solution/gong-shui-san-xie-xiang-jie-li-yong-er-w-ai0d/) | 中等 | 🤩🤩🤩 |
7777
| [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/by-ac_oier-wgr8/) | 中等 | 🤩🤩🤩🤩 |
7878
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
79+
| [面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/) | 困难 | 🤩🤩🤩🤩 |
7980

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/)** ,难度为 **困难**
4+
5+
Tag : 「数学」
6+
7+
8+
9+
给定一个数组,包含从 `1``N` 所有的整数,但其中缺了两个数字。你能在 $O(N)$ 时间内只用 $O(1)$ 的空间找到它们吗?
10+
11+
以任意顺序返回这两个数字均可。
12+
13+
示例 1:
14+
```
15+
输入: [1]
16+
17+
输出: [2,3]
18+
```
19+
示例 2:
20+
```
21+
输入: [2,3]
22+
23+
输出: [1,4]
24+
```
25+
提示:
26+
* $nums.length <= 30000$
27+
28+
---
29+
30+
### 数学
31+
32+
根据题意,给定 `nums` 的长度为 $m$ 且缺失了两个数,所有的 $nums[i]$ 加上缺失数字组成连续排列长度为 $n = m + 2$。
33+
34+
根据等差数量求和公式可知,补齐后的排列总和为 $\frac{n \times (1 + n)}{2}$,补全后的理论总和与实际总和之间的差值 $cur = \frac{n \times (1 + n)}{2} - \sum_{i = 0}^{m - 1}nums[i]$ 为缺失数值之和。
35+
36+
根据补全后数值各不相同可知,两者必不可能同时位于 $t = \left \lfloor \frac{cur}{2} \right \rfloor$ 的同一侧(偏大、偏小或数值重复),因此我们可以计算 $[1, t]$ 范围内的理论总和与实际总和之间的差值来确定其一(将问题转换为求解缺失一值),再结合缺失两值之和 $sum$ 算得答案。
37+
38+
Java 代码:
39+
```Java
40+
class Solution {
41+
public int[] missingTwo(int[] nums) {
42+
int n = nums.length + 2, cur = n * (1 + n) / 2;
43+
for (int x : nums) cur -= x;
44+
int sum = cur, t = cur / 2;
45+
cur = t * (1 + t) / 2;
46+
for (int x : nums) {
47+
if (x <= t) cur -= x;
48+
}
49+
return new int[]{cur, sum - cur};
50+
}
51+
}
52+
```
53+
TypeScript 代码:
54+
```TypeScript
55+
function missingTwo(nums: number[]): number[] {
56+
let n = nums.length + 2, cur = Math.floor(n * (1 + n) / 2)
57+
for (let x of nums) cur -= x
58+
let sum = cur, t = Math.floor(cur / 2)
59+
cur = Math.floor(t * (1 + t) / 2)
60+
for (let x of nums) {
61+
if (x <= t) cur -= x
62+
}
63+
return [cur, sum - cur]
64+
};
65+
```
66+
Python 代码:
67+
```Python
68+
class Solution:
69+
def missingTwo(self, nums: List[int]) -> List[int]:
70+
n = len(nums) + 2
71+
cur = n * (1 + n) // 2 - sum(nums)
72+
tot, t = cur, cur // 2
73+
cur = t * (1 + t) // 2 - sum([x for x in nums if x <= t])
74+
return [cur, tot - cur]
75+
```
76+
* 时间复杂度:$O(n)$
77+
* 空间复杂度:$O(1)$
78+
79+
---
80+
81+
### 最后
82+
83+
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 17.19` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
84+
85+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
86+
87+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
88+
89+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
90+

0 commit comments

Comments
 (0)