Skip to content

Commit ac078ef

Browse files
committed
补充第 200 ~ 299 题的题目解析(增加 8 道题)
1 parent f25ca37 commit ac078ef

11 files changed

+958
-1
lines changed

docs/00_preface/00_05_solutions_list.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 886 道)
1+
# LeetCode 题解(已完成 894 道)
22

33
### 第 1 ~ 99 题
44

@@ -212,6 +212,7 @@
212212
| [0212. 单词搜索 II](https://leetcode.cn/problems/word-search-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/word-search-ii.md) | 字典树、数组、字符串、回溯、矩阵 | 困难 |
213213
| [0213. 打家劫舍 II](https://leetcode.cn/problems/house-robber-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/house-robber-ii.md) | 数组、动态规划 | 中等 |
214214
| [0215. 数组中的第K个最大元素](https://leetcode.cn/problems/kth-largest-element-in-an-array/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/kth-largest-element-in-an-array.md) | 数组、分治、快速选择、排序、堆(优先队列) | 中等 |
215+
| [0216. 组合总和 III](https://leetcode.cn/problems/combination-sum-iii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/combination-sum-iii.md) | 数组、回溯 | 中等 |
215216
| [0217. 存在重复元素](https://leetcode.cn/problems/contains-duplicate/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/contains-duplicate.md) | 数组、哈希表、排序 | 简单 |
216217
| [0218. 天际线问题](https://leetcode.cn/problems/the-skyline-problem/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/the-skyline-problem.md) | 树状数组、线段树、数组、分治、有序集合、排序、扫描线、堆(优先队列) | 困难 |
217218
| [0219. 存在重复元素 II](https://leetcode.cn/problems/contains-duplicate-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/contains-duplicate-ii.md) | 数组、哈希表、滑动窗口 | 简单 |
@@ -235,6 +236,8 @@
235236
| [0241. 为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/different-ways-to-add-parentheses.md) | 递归、记忆化搜索、数学、字符串、动态规划 | 中等 |
236237
| [0242. 有效的字母异位词](https://leetcode.cn/problems/valid-anagram/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/valid-anagram.md) | 哈希表、字符串、排序 | 简单 |
237238
| [0249. 移位字符串分组](https://leetcode.cn/problems/group-shifted-strings/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/group-shifted-strings.md) | 数组、哈希表、字符串 | 中等 |
239+
| [0250. 统计同值子树](https://leetcode.cn/problems/count-univalue-subtrees/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/count-univalue-subtrees.md) | 树、深度优先搜索、二叉树 | 中等 |
240+
| [0254. 因子的组合](https://leetcode.cn/problems/factor-combinations/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/factor-combinations.md) | 回溯 | 中等 |
238241
| [0257. 二叉树的所有路径](https://leetcode.cn/problems/binary-tree-paths/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/binary-tree-paths.md) | 树、深度优先搜索、字符串、回溯、二叉树 | 简单 |
239242
| [0258. 各位相加](https://leetcode.cn/problems/add-digits/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/add-digits.md) | 数学、数论、模拟 | 简单 |
240243
| [0259. 较小的三数之和](https://leetcode.cn/problems/3sum-smaller/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/3sum-smaller.md) | 数组、双指针、二分查找、排序 | 中等 |
@@ -243,6 +246,9 @@
243246
| [0264. 丑数 II](https://leetcode.cn/problems/ugly-number-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/ugly-number-ii.md) | 哈希表、数学、动态规划、堆(优先队列) | 中等 |
244247
| [0268. 丢失的数字](https://leetcode.cn/problems/missing-number/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/missing-number.md) | 位运算、数组、哈希表、数学、二分查找、排序 | 简单 |
245248
| [0270. 最接近的二叉搜索树值](https://leetcode.cn/problems/closest-binary-search-tree-value/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/closest-binary-search-tree-value.md) | 树、深度优先搜索、二叉搜索树、二分查找、二叉树 | 简单 |
249+
| [0271. 字符串的编码与解码](https://leetcode.cn/problems/encode-and-decode-strings/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/encode-and-decode-strings.md) | 设计、数组、字符串 | 中等 |
250+
| [0272. 最接近的二叉搜索树值 II](https://leetcode.cn/problems/closest-binary-search-tree-value-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/closest-binary-search-tree-value-ii.md) | 栈、树、深度优先搜索、二叉搜索树、双指针、二叉树、堆(优先队列) | 困难 |
251+
| [0277. 搜寻名人](https://leetcode.cn/problems/find-the-celebrity/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/find-the-celebrity.md) | 图、双指针、交互 | 中等 |
246252
| [0278. 第一个错误的版本](https://leetcode.cn/problems/first-bad-version/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/first-bad-version.md) | 二分查找、交互 | 简单 |
247253
| [0279. 完全平方数](https://leetcode.cn/problems/perfect-squares/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/perfect-squares.md) | 广度优先搜索、数学、动态规划 | 中等 |
248254
| [0283. 移动零](https://leetcode.cn/problems/move-zeroes/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/move-zeroes.md) | 数组、双指针 | 简单 |
@@ -255,6 +261,8 @@
255261
| [0292. Nim 游戏](https://leetcode.cn/problems/nim-game/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/nim-game.md) | 脑筋急转弯、数学、博弈 | 简单 |
256262
| [0295. 数据流的中位数](https://leetcode.cn/problems/find-median-from-data-stream/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/find-median-from-data-stream.md) | 设计、双指针、数据流、排序、堆(优先队列) | 困难 |
257263
| [0297. 二叉树的序列化与反序列化](https://leetcode.cn/problems/serialize-and-deserialize-binary-tree/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/serialize-and-deserialize-binary-tree.md) | 树、深度优先搜索、广度优先搜索、设计、字符串、二叉树 | 困难 |
264+
| [0298. 二叉树最长连续序列](https://leetcode.cn/problems/binary-tree-longest-consecutive-sequence/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/binary-tree-longest-consecutive-sequence.md) | 树、深度优先搜索、二叉树 | 中等 |
265+
| [0299. 猜数字游戏](https://leetcode.cn/problems/bulls-and-cows/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0200-0299/bulls-and-cows.md) | 哈希表、字符串、计数 | 中等 |
258266

259267

260268
### 第 300 ~ 399 题

docs/others/update_time.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2025-10
22

3+
- 2025-10-19 补充第 200 ~ 299 题的题目解析(增加 8 道题)
34
- 2025-10-17 补全第 100 ~ 199 题的题目解析(增加 12 道题)
45
- 2025-10-16 补全第 1 ~ 99 题的题目解析(增加 13 道题)
56
- 2025-10-13 编写生成 PDF 脚本,并生成 AlgoNote-v2.0.pdf
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [0298. 二叉树最长连续序列](https://leetcode.cn/problems/binary-tree-longest-consecutive-sequence/)
2+
3+
- 标签:树、深度优先搜索、二叉树
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [0298. 二叉树最长连续序列 - 力扣](https://leetcode.cn/problems/binary-tree-longest-consecutive-sequence/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
给你一棵指定的二叉树的根节点 $root$。
15+
16+
**要求**
17+
18+
计算其中「最长连续序列路径」的长度。
19+
20+
**说明**
21+
22+
- 「最长连续序列路径」是依次递增 $1$ 的路径。该路径,可以是从某个初始节点到树中任意节点,通过「父 - 子」关系连接而产生的任意路径。且必须从父节点到子节点,反过来是不可以的。
23+
- 树中节点的数目在范围 $[1, 3 \times 10^{4}]$ 内。
24+
- $-3 \times 10^{4} \le Node.val \le 3 \times 10^{4}$。
25+
26+
**示例**
27+
28+
- 示例 1:
29+
30+
![](https://assets.leetcode.com/uploads/2021/03/14/consec1-1-tree.jpg)
31+
32+
```python
33+
输入:root = [1,null,3,2,4,null,null,null,5]
34+
输出:3
35+
解释:当中,最长连续序列是 3-4-5 ,所以返回结果为 3
36+
```
37+
38+
- 示例 2:
39+
40+
![](https://assets.leetcode.com/uploads/2021/03/14/consec1-2-tree.jpg)
41+
42+
```python
43+
输入:root = [2,null,3,2,null,1]
44+
输出:2
45+
解释:当中,最长连续序列是 2-3 。注意,不是 3-2-1,所以返回 2
46+
```
47+
48+
## 解题思路
49+
50+
### 思路 1:深度优先搜索
51+
52+
这是一个典型的树形动态规划问题。我们需要找到二叉树中最长的连续递增序列路径。
53+
54+
我们可以使用深度优先搜索来解决这个问题:
55+
56+
1. **明确问题**:对于每个节点,我们需要计算以该节点为起点的最长连续序列长度。
57+
2. **状态定义**:定义 $dfs(node)$ 函数,返回以 $node$ 为起点的最长连续序列长度。
58+
3. **状态转移**
59+
- 如果 $node.left$ 存在且 $node.left.val = node.val + 1$,则左子树可以延续当前序列。
60+
- 如果 $node.right$ 存在且 $node.right.val = node.val + 1$,则右子树可以延续当前序列。
61+
- 取左右子树中的最大值,加上当前节点(长度为 $1$)。
62+
4. **全局最优**:在遍历过程中,维护全局最长序列长度 $max\_length$。
63+
64+
### 思路 1:代码
65+
66+
```python
67+
# Definition for a binary tree node.
68+
# class TreeNode:
69+
# def __init__(self, val=0, left=None, right=None):
70+
# self.val = val
71+
# self.left = left
72+
# self.right = right
73+
class Solution:
74+
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
75+
self.max_length = 0 # 全局最长序列长度
76+
77+
def dfs(node):
78+
if not node:
79+
return 0
80+
81+
# 计算以当前节点为起点的最长连续序列长度
82+
current_length = 1
83+
84+
# 递归计算左右子树
85+
left_length = dfs(node.left)
86+
right_length = dfs(node.right)
87+
88+
# 如果左子节点可以延续当前序列
89+
if node.left and node.left.val == node.val + 1:
90+
current_length = max(current_length, left_length + 1)
91+
92+
# 如果右子节点可以延续当前序列
93+
if node.right and node.right.val == node.val + 1:
94+
current_length = max(current_length, right_length + 1)
95+
96+
# 更新全局最长序列长度
97+
self.max_length = max(self.max_length, current_length)
98+
99+
return current_length
100+
101+
dfs(root) # 从根节点开始深度优先搜索
102+
return self.max_length
103+
```
104+
105+
### 思路 1:复杂度分析
106+
107+
- **时间复杂度**:$O(n)$,其中 $n$ 是树中节点的数量。每个节点都会被访问一次。
108+
- **空间复杂度**:$O(h)$,其中 $h$ 是树的高度。递归调用栈的深度最多为树的高度。
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [0299. 猜数字游戏](https://leetcode.cn/problems/bulls-and-cows/)
2+
3+
- 标签:哈希表、字符串、计数
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [0299. 猜数字游戏 - 力扣](https://leetcode.cn/problems/bulls-and-cows/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
你在和朋友一起玩 猜数字(Bulls and Cows)游戏,该游戏规则如下:
15+
16+
写出一个秘密数字,并请朋友猜这个数字是多少。朋友每猜测一次,你就会给他一个包含下述信息的提示:
17+
18+
- 猜测数字中有多少位属于数字和确切位置都猜对了(称为 `"Bulls"`,公牛),
19+
- 有多少位属于数字猜对了但是位置不对(称为 `"Cows"`,奶牛)。也就是说,这次猜测中有多少位非公牛数字可以通过重新排列转换成公牛数字。
20+
21+
给定一个秘密数字 $secret$ 和朋友猜测的数字 $guess$。
22+
23+
**要求**
24+
25+
请你返回对朋友这次猜测的提示。
26+
27+
提示的格式为 `"xAyB"`,$x$ 是公牛个数, $y$ 是奶牛个数,$A$ 表示公牛,$B$ 表示奶牛。
28+
29+
请注意秘密数字和朋友猜测的数字都可能含有重复数字。
30+
31+
**说明**
32+
33+
- $1 \le secret.length, guess.length \le 10^{3}$。
34+
- $secret.length == guess.length$。
35+
- $secret$ 和 $guess$ 仅由数字组成。
36+
37+
**示例**
38+
39+
- 示例 1:
40+
41+
```python
42+
输入:secret = "1807", guess = "7810"
43+
输出:"1A3B"
44+
解释:数字和位置都对(公牛)用 '|' 连接,数字猜对位置不对(奶牛)的采用斜体加粗标识。
45+
"1807"
46+
|
47+
"7810"
48+
```
49+
50+
- 示例 2:
51+
52+
```python
53+
输入:secret = "1123", guess = "0111"
54+
输出:"1A1B"
55+
解释:数字和位置都对(公牛)用 '|' 连接,数字猜对位置不对(奶牛)的采用斜体加粗标识。
56+
"1123" "1123"
57+
| or |
58+
"0111" "0111"
59+
注意,两个不匹配的 1 中,只有一个会算作奶牛(数字猜对位置不对)。通过重新排列非公牛数字,其中仅有一个 1 可以成为公牛数字。
60+
```
61+
62+
## 解题思路
63+
64+
### 思路 1:哈希表计数
65+
66+
这是一个典型的计数问题。我们需要分别统计 Bulls 和 Cows 的数量。
67+
68+
**算法步骤**
69+
70+
1. **统计 Bulls**:遍历 $secret$ 和 $guess$,如果 $secret[i] = guess[i]$,则 $bulls$ 计数加 $1$。
71+
72+
2. **统计 Cows**
73+
- 使用哈希表 $secret\_count$ 统计 $secret$ 中每个数字的出现次数(排除 Bulls 位置)。
74+
- 使用哈希表 $guess\_count$ 统计 $guess$ 中每个数字的出现次数(排除 Bulls 位置)。
75+
- 对于每个数字 $digit$,$cows$ 加上 $min(secret\_count[digit], guess\_count[digit])$。
76+
77+
3. **返回结果**:返回格式为 $f"{bulls}A{cows}B"$。
78+
79+
**关键点**
80+
81+
- Bulls 位置的数字不能重复计算为 Cows。
82+
- 对于重复数字,Cows 的数量是两者中较小值。
83+
84+
### 思路 1:代码
85+
86+
```python
87+
class Solution:
88+
def getHint(self, secret: str, guess: str) -> str:
89+
bulls = 0 # Bulls 计数:位置和数字都正确
90+
cows = 0 # Cows 计数:数字正确但位置错误
91+
92+
# 统计 Bulls
93+
secret_chars = list(secret)
94+
guess_chars = list(guess)
95+
96+
for i in range(len(secret)):
97+
if secret_chars[i] == guess_chars[i]:
98+
bulls += 1
99+
# 将 Bulls 位置的字符标记为已使用,避免重复计算
100+
secret_chars[i] = 'X'
101+
guess_chars[i] = 'X'
102+
103+
# 统计 Cows
104+
secret_count = {} # secret 中每个数字的出现次数(排除 Bulls)
105+
guess_count = {} # guess 中每个数字的出现次数(排除 Bulls)
106+
107+
# 统计 secret 中非 Bulls 位置的数字
108+
for char in secret_chars:
109+
if char != 'X':
110+
secret_count[char] = secret_count.get(char, 0) + 1
111+
112+
# 统计 guess 中非 Bulls 位置的数字
113+
for char in guess_chars:
114+
if char != 'X':
115+
guess_count[char] = guess_count.get(char, 0) + 1
116+
117+
# 计算 Cows:对于每个数字,取两者中的较小值
118+
for digit in secret_count:
119+
if digit in guess_count:
120+
cows += min(secret_count[digit], guess_count[digit])
121+
122+
return f"{bulls}A{cows}B"
123+
```
124+
125+
### 思路 1:复杂度分析
126+
127+
- **时间复杂度**:$O(n)$,其中 $n$ 是字符串长度。需要遍历字符串两次,哈希表操作的时间复杂度为 $O(1)$。
128+
- **空间复杂度**:$O(1)$,因为数字只有 $0-9$ 共 $10$ 种,哈希表最多存储 $10$ 个键值对,空间复杂度为常数。
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [0272. 最接近的二叉搜索树值 II](https://leetcode.cn/problems/closest-binary-search-tree-value-ii/)
2+
3+
- 标签:栈、树、深度优先搜索、二叉搜索树、双指针、二叉树、堆(优先队列)
4+
- 难度:困难
5+
6+
## 题目链接
7+
8+
- [0272. 最接近的二叉搜索树值 II - 力扣](https://leetcode.cn/problems/closest-binary-search-tree-value-ii/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
给定二叉搜索树的根 $root$ 、一个目标值 $target$ 和一个整数 $k$。
15+
16+
**要求**
17+
18+
返回 BST 中最接近目标的 $k$ 个值。你可以按任意顺序返回答案。
19+
20+
**说明**
21+
22+
- 题目保证该二叉搜索树中只会存在一种 $k$ 个值集合最接近 $target$。
23+
- 二叉树的节点总数为 n。
24+
- $1 \le k \le n \le 10^{4}$。
25+
- $0 \le Node.val \le 10^{9}$。
26+
- $-10^{9} \le target \le 10^{9}$。
27+
28+
- 进阶:假设该二叉搜索树是平衡的,请问您是否能在小于 O(n)( n = total nodes )的时间复杂度内解决该问题呢?
29+
30+
**示例**
31+
32+
- 示例 1:
33+
34+
![](https://assets.leetcode.com/uploads/2021/03/12/closest1-1-tree.jpg)
35+
36+
```python
37+
输入: root = [4,2,5,1,3],目标值 = 3.714286,且 k = 2
38+
输出: [4,3]
39+
```
40+
41+
- 示例 2:
42+
43+
```python
44+
输入: root = [1], target = 0.000000, k = 1
45+
输出: [1]
46+
```
47+
48+
## 解题思路
49+
50+
### 思路 1:中序遍历 + 排序
51+
52+
这是一个典型的二叉搜索树问题。我们需要找到 BST 中最接近目标值 $target$ 的 $k$ 个值。
53+
54+
我们可以使用中序遍历 + 排序的方法来解决这个问题:
55+
56+
1. **中序遍历**:由于 BST 的中序遍历结果是有序的,我们可以先通过中序遍历得到所有节点的值,存储在数组 $values$ 中。
57+
2. **按距离排序**:将所有节点值按照与 $target$ 的距离进行排序,选择距离最小的 $k$ 个值。
58+
3. **算法步骤**
59+
- 对 BST 进行中序遍历,得到有序数组 $values$。
60+
- 创建一个包含节点值和距离的列表 $distances$,其中每个元素为 $(|value - target|, value)$。
61+
- 对 $distances$ 按照距离进行排序。
62+
- 取前 $k$ 个元素的值作为结果。
63+
64+
### 思路 1:代码
65+
66+
```python
67+
# Definition for a binary tree node.
68+
# class TreeNode:
69+
# def __init__(self, val=0, left=None, right=None):
70+
# self.val = val
71+
# self.left = left
72+
# self.right = right
73+
class Solution:
74+
def closestKValues(self, root: Optional[TreeNode], target: float, k: int) -> List[int]:
75+
values = [] # 存储中序遍历的结果
76+
77+
def inorder(node):
78+
"""中序遍历 BST,得到有序数组"""
79+
if not node:
80+
return
81+
inorder(node.left) # 遍历左子树
82+
values.append(node.val) # 访问根节点
83+
inorder(node.right) # 遍历右子树
84+
85+
inorder(root) # 执行中序遍历
86+
87+
# 创建距离列表,每个元素为 (距离, 值)
88+
distances = []
89+
for value in values:
90+
distance = abs(value - target)
91+
distances.append((distance, value))
92+
93+
# 按距离排序,取前 k 个值
94+
distances.sort()
95+
result = [distances[i][1] for i in range(k)]
96+
97+
return result
98+
```
99+
100+
### 思路 1:复杂度分析
101+
102+
- **时间复杂度**:$O(n \log n)$,其中 $n$ 是 BST 中节点的数量。需要遍历所有节点进行中序遍历,然后对距离进行排序。
103+
- **空间复杂度**:$O(n)$,需要存储中序遍历的结果数组、距离列表,以及递归调用栈的空间。

0 commit comments

Comments
 (0)