Skip to content

Commit c55ca0e

Browse files
committed
补充第 300 ~ 399 题的题目解析(增加 13 道题)
1 parent 254fed8 commit c55ca0e

17 files changed

+1821
-2
lines changed

docs/00_preface/00_05_solutions_list.md

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

33
### 第 1 ~ 99 题
44

@@ -305,17 +305,22 @@
305305
| [0300. 最长递增子序列](https://leetcode.cn/problems/longest-increasing-subsequence/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-increasing-subsequence.md) | 数组、二分查找、动态规划 | 中等 |
306306
| [0303. 区域和检索 - 数组不可变](https://leetcode.cn/problems/range-sum-query-immutable/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-immutable.md) | 设计、数组、前缀和 | 简单 |
307307
| [0304. 二维区域和检索 - 矩阵不可变](https://leetcode.cn/problems/range-sum-query-2d-immutable/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-2d-immutable.md) | 设计、数组、矩阵、前缀和 | 中等 |
308+
| [0306. 累加数](https://leetcode.cn/problems/additive-number/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/additive-number.md) | 字符串、回溯 | 中等 |
308309
| [0307. 区域和检索 - 数组可修改](https://leetcode.cn/problems/range-sum-query-mutable/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-sum-query-mutable.md) | 设计、树状数组、线段树、数组、分治 | 中等 |
309310
| [0309. 买卖股票的最佳时机含冷冻期](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/best-time-to-buy-and-sell-stock-with-cooldown.md) | 数组、动态规划 | 中等 |
310311
| [0310. 最小高度树](https://leetcode.cn/problems/minimum-height-trees/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/minimum-height-trees.md) | 深度优先搜索、广度优先搜索、图、拓扑排序 | 中等 |
311312
| [0312. 戳气球](https://leetcode.cn/problems/burst-balloons/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/burst-balloons.md) | 数组、动态规划 | 困难 |
313+
| [0314. 二叉树的垂直遍历](https://leetcode.cn/problems/binary-tree-vertical-order-traversal/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/binary-tree-vertical-order-traversal.md) | 树、深度优先搜索、广度优先搜索、哈希表、二叉树、排序 | 中等 |
312314
| [0315. 计算右侧小于当前元素的个数](https://leetcode.cn/problems/count-of-smaller-numbers-after-self/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-of-smaller-numbers-after-self.md) | 树状数组、线段树、数组、二分查找、分治、有序集合、归并排序 | 困难 |
313315
| [0316. 去除重复字母](https://leetcode.cn/problems/remove-duplicate-letters/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/remove-duplicate-letters.md) | 栈、贪心、字符串、单调栈 | 中等 |
314316
| [0318. 最大单词长度乘积](https://leetcode.cn/problems/maximum-product-of-word-lengths/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/maximum-product-of-word-lengths.md) | 位运算、数组、字符串 | 中等 |
317+
| [0319. 灯泡开关](https://leetcode.cn/problems/bulb-switcher/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/bulb-switcher.md) | 脑筋急转弯、数学 | 中等 |
318+
| [0321. 拼接最大数](https://leetcode.cn/problems/create-maximum-number/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/create-maximum-number.md) | 栈、贪心、数组、双指针、单调栈 | 困难 |
315319
| [0322. 零钱兑换](https://leetcode.cn/problems/coin-change/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/coin-change.md) | 广度优先搜索、数组、动态规划 | 中等 |
316320
| [0323. 无向图中连通分量的数目](https://leetcode.cn/problems/number-of-connected-components-in-an-undirected-graph/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/number-of-connected-components-in-an-undirected-graph.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 |
317321
| [0324. 摆动排序 II](https://leetcode.cn/problems/wiggle-sort-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/wiggle-sort-ii.md) | 贪心、数组、分治、快速选择、排序 | 中等 |
318322
| [0326. 3 的幂](https://leetcode.cn/problems/power-of-three/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/power-of-three.md) | 递归、数学 | 简单 |
323+
| [0327. 区间和的个数](https://leetcode.cn/problems/count-of-range-sum/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-of-range-sum.md) | 树状数组、线段树、数组、二分查找、分治、有序集合、归并排序 | 困难 |
319324
| [0328. 奇偶链表](https://leetcode.cn/problems/odd-even-linked-list/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/odd-even-linked-list.md) | 链表 | 中等 |
320325
| [0329. 矩阵中的最长递增路径](https://leetcode.cn/problems/longest-increasing-path-in-a-matrix/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/longest-increasing-path-in-a-matrix.md) | 深度优先搜索、广度优先搜索、图、拓扑排序、记忆化搜索、数组、动态规划、矩阵 | 困难 |
321326
| [0334. 递增的三元子序列](https://leetcode.cn/problems/increasing-triplet-subsequence/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/increasing-triplet-subsequence.md) | 贪心、数组 | 中等 |
@@ -330,13 +335,19 @@
330335
| [0345. 反转字符串中的元音字母](https://leetcode.cn/problems/reverse-vowels-of-a-string/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/reverse-vowels-of-a-string.md) | 双指针、字符串 | 简单 |
331336
| [0346. 数据流中的移动平均值](https://leetcode.cn/problems/moving-average-from-data-stream/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/moving-average-from-data-stream.md) | 设计、队列、数组、数据流 | 简单 |
332337
| [0347. 前 K 个高频元素](https://leetcode.cn/problems/top-k-frequent-elements/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/top-k-frequent-elements.md) | 数组、哈希表、分治、桶排序、计数、快速选择、排序、堆(优先队列) | 中等 |
338+
| [0348. 设计井字棋](https://leetcode.cn/problems/design-tic-tac-toe/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-tic-tac-toe.md) | 设计、数组、哈希表、矩阵、模拟 | 中等 |
333339
| [0349. 两个数组的交集](https://leetcode.cn/problems/intersection-of-two-arrays/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/intersection-of-two-arrays.md) | 数组、哈希表、双指针、二分查找、排序 | 简单 |
334340
| [0350. 两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/intersection-of-two-arrays-ii.md) | 数组、哈希表、双指针、二分查找、排序 | 简单 |
335341
| [0351. 安卓系统手势解锁](https://leetcode.cn/problems/android-unlock-patterns/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/android-unlock-patterns.md) | 位运算、动态规划、回溯、状态压缩 | 中等 |
342+
| [0352. 将数据流变为多个不相交区间](https://leetcode.cn/problems/data-stream-as-disjoint-intervals/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/data-stream-as-disjoint-intervals.md) | 设计、二分查找、有序集合 | 困难 |
343+
| [0353. 贪吃蛇](https://leetcode.cn/problems/design-snake-game/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-snake-game.md) | 设计、队列、数组、哈希表、模拟 | 中等 |
336344
| [0354. 俄罗斯套娃信封问题](https://leetcode.cn/problems/russian-doll-envelopes/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/russian-doll-envelopes.md) | 数组、二分查找、动态规划、排序 | 困难 |
345+
| [0355. 设计推特](https://leetcode.cn/problems/design-twitter/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-twitter.md) | 设计、哈希表、链表、堆(优先队列) | 中等 |
337346
| [0357. 统计各位数字都不同的数字个数](https://leetcode.cn/problems/count-numbers-with-unique-digits/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/count-numbers-with-unique-digits.md) | 数学、动态规划、回溯 | 中等 |
338347
| [0359. 日志速率限制器](https://leetcode.cn/problems/logger-rate-limiter/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/logger-rate-limiter.md) | 设计、哈希表、数据流 | 简单 |
339348
| [0360. 有序转化数组](https://leetcode.cn/problems/sort-transformed-array/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sort-transformed-array.md) | 数组、数学、双指针、排序 | 中等 |
349+
| [0361. 轰炸敌人](https://leetcode.cn/problems/bomb-enemy/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/bomb-enemy.md) | 数组、动态规划、矩阵 | 中等 |
350+
| [0362. 敲击计数器](https://leetcode.cn/problems/design-hit-counter/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-hit-counter.md) | 设计、队列、数组、二分查找、数据流 | 中等 |
340351
| [0367. 有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/valid-perfect-square.md) | 数学、二分查找 | 简单 |
341352
| [0370. 区间加法](https://leetcode.cn/problems/range-addition/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/range-addition.md) | 数组、前缀和 | 中等 |
342353
| [0371. 两整数之和](https://leetcode.cn/problems/sum-of-two-integers/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/sum-of-two-integers.md) | 位运算、数学 | 中等 |
@@ -345,12 +356,14 @@
345356
| [0376. 摆动序列](https://leetcode.cn/problems/wiggle-subsequence/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/wiggle-subsequence.md) | 贪心、数组、动态规划 | 中等 |
346357
| [0377. 组合总和 Ⅳ](https://leetcode.cn/problems/combination-sum-iv/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/combination-sum-iv.md) | 数组、动态规划 | 中等 |
347358
| [0378. 有序矩阵中第 K 小的元素](https://leetcode.cn/problems/kth-smallest-element-in-a-sorted-matrix/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/kth-smallest-element-in-a-sorted-matrix.md) | 数组、二分查找、矩阵、排序、堆(优先队列) | 中等 |
359+
| [0379. 电话目录管理系统](https://leetcode.cn/problems/design-phone-directory/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/design-phone-directory.md) | 设计、队列、数组、哈希表、链表 | 中等 |
348360
| [0380. O(1) 时间插入、删除和获取随机元素](https://leetcode.cn/problems/insert-delete-getrandom-o1/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/insert-delete-getrandom-o1.md) | 设计、数组、哈希表、数学、随机化 | 中等 |
349361
| [0383. 赎金信](https://leetcode.cn/problems/ransom-note/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/ransom-note.md) | 哈希表、字符串、计数 | 简单 |
350362
| [0384. 打乱数组](https://leetcode.cn/problems/shuffle-an-array/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/shuffle-an-array.md) | 设计、数组、数学、随机化 | 中等 |
351363
| [0386. 字典序排数](https://leetcode.cn/problems/lexicographical-numbers/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/lexicographical-numbers.md) | 深度优先搜索、字典树 | 中等 |
352364
| [0387. 字符串中的第一个唯一字符](https://leetcode.cn/problems/first-unique-character-in-a-string/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/first-unique-character-in-a-string.md) | 队列、哈希表、字符串、计数 | 简单 |
353365
| [0389. 找不同](https://leetcode.cn/problems/find-the-difference/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/find-the-difference.md) | 位运算、哈希表、字符串、排序 | 简单 |
366+
| [0390. 消除游戏](https://leetcode.cn/problems/elimination-game/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/elimination-game.md) | 递归、数学 | 中等 |
354367
| [0391. 完美矩形](https://leetcode.cn/problems/perfect-rectangle/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/perfect-rectangle.md) | 几何、数组、哈希表、数学、扫描线 | 困难 |
355368
| [0392. 判断子序列](https://leetcode.cn/problems/is-subsequence/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/is-subsequence.md) | 双指针、字符串、动态规划 | 简单 |
356369
| [0394. 字符串解码](https://leetcode.cn/problems/decode-string/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0300-0399/decode-string.md) | 栈、递归、字符串 | 中等 |

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-21 补充第 300 ~ 399 题的题目解析(增加 13 道题)
34
- 2025-10-20 补全第 200 ~ 299 题的题目解析(增加 33 道题)
45
- 2025-10-19 补充第 200 ~ 299 题的题目解析(增加 8 道题)
56
- 2025-10-17 补全第 100 ~ 199 题的题目解析(增加 12 道题)

docs/solutions/0200-0299/palindrome-permutation-ii.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,5 @@ class Solution:
102102

103103
### 思路 1:复杂度分析
104104

105-
- **时间复杂度**:令 $m = \sum_i \lfloor c_i/2 \rfloor$。半串的唯一排列数为 $\dfrac{m!}{\prod_i (\lfloor c_i/2 \rfloor)!}$。对每个半串构造完整回文需要 $O(n)$ 拼接与拷贝,因此总复杂度为 $O\!\left(\dfrac{m!}{\prod_i (\lfloor c_i/2 \rfloor)!} \cdot n\right)$。在 $n \le 16$ 的约束下不会超时。
105+
- **时间复杂度**:令 $m = \sum_i \lfloor c_i/2 \rfloor$。半串的唯一排列数为 $\dfrac{m!}{\prod_i (\lfloor c_i/2 \rfloor)!}$。对每个半串构造完整回文需要 $O(n)$ 拼接与拷贝,因此总复杂度为 $O\!\left(\dfrac{m!}{\prod_i (\lfloor c_i/2 \rfloor)!} \times n\right)$。在 $n \le 16$ 的约束下不会超时。
106106
- **空间复杂度**:回溯深度与路径为 $O(m)$,计数表为 $O(\Sigma)$($\Sigma$ 为字符集大小,这里为小写字母)。不计输出的额外空间为 $O(m + \Sigma)$。
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# [0306. 累加数](https://leetcode.cn/problems/additive-number/)
2+
3+
- 标签:字符串、回溯
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [0306. 累加数 - 力扣](https://leetcode.cn/problems/additive-number/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
「累加数」是一个字符串,组成它的数字可以形成累加序列。
15+
一个有效的 累加序列 必须「至少」包含 $3$ 个数。除了最开始的两个数以外,序列中的每个后续数字必须是它之前两个数字之和。
16+
17+
给定一个只包含数字 $0 \sim 9$ 的字符串。
18+
19+
**要求**
20+
21+
编写一个算法来判断给定输入是否是「累加数 」。如果是,返回 $true$;否则,返回 $false$。
22+
23+
**说明**
24+
25+
- 累加序列里的数,除数字 $0$ 之外,不会以 $0$ 开头,所以不会出现 $1, 2, 03$ 或者 $1, 02, 3$ 的情况。
26+
- $1 \le num.length \le 35$。
27+
- $num$ 仅由数字 $0 sim 9$ 组成。
28+
29+
- 进阶:你计划如何处理由过大的整数输入导致的溢出?
30+
31+
**示例**
32+
33+
- 示例 1:
34+
35+
```python
36+
输入:"112358"
37+
输出:true
38+
解释:累加序列为: 1, 1, 2, 3, 5, 81 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8
39+
```
40+
41+
- 示例 2:
42+
43+
```python
44+
输入:"199100199"
45+
输出:true
46+
解释:累加序列为: 1, 99, 100, 1991 + 99 = 100, 99 + 100 = 199
47+
```
48+
49+
## 解题思路
50+
51+
### 思路 1:回溯算法
52+
53+
累加数问题可以通过回溯算法来解决。我们需要找到字符串中所有可能的数字分割方式,使得分割后的数字序列满足累加数的定义。
54+
55+
**问题分析**
56+
57+
对于字符串 $num$,我们需要:
58+
59+
- 确定前两个数字 $a$ 和 $b$。
60+
- 验证后续数字是否满足 $c = a + b$ 的关系。
61+
- 递归验证整个序列。
62+
63+
**算法步骤**
64+
65+
1. **枚举前两个数字**:使用双重循环枚举所有可能的前两个数字 $a$ 和 $b$ 的起始位置。
66+
2. **验证数字有效性**:确保数字不以 $0$ 开头(除非数字本身就是 $0$)。
67+
3. **递归验证序列**:从第三个数字开始,验证每个数字是否等于前两个数字的和。
68+
4. **回溯剪枝**:如果某个分支不满足条件,立即返回 $false$。
69+
70+
###### 3. 关键变量
71+
72+
- $i$:第一个数字的结束位置。
73+
- $j$:第二个数字的结束位置。
74+
- $a$:第一个数字的值。
75+
- $b$:第二个数字的值。
76+
- $sum$:前两个数字的和,用于验证第三个数字。
77+
78+
### 思路 1:代码
79+
80+
```python
81+
class Solution:
82+
def isAdditiveNumber(self, num: str) -> bool:
83+
n = len(num)
84+
85+
# 枚举前两个数字的所有可能位置
86+
for i in range(1, n):
87+
for j in range(i + 1, n):
88+
# 获取第一个数字
89+
first = num[:i]
90+
# 获取第二个数字
91+
second = num[i:j]
92+
93+
# 检查数字是否有效(不能以0开头,除非数字本身就是0)
94+
if (len(first) > 1 and first[0] == '0') or \
95+
(len(second) > 1 and second[0] == '0'):
96+
continue
97+
98+
# 转换为整数
99+
a = int(first)
100+
b = int(second)
101+
102+
# 递归验证剩余部分
103+
if self._isValid(num, j, a, b):
104+
return True
105+
106+
return False
107+
108+
def _isValid(self, num: str, start: int, a: int, b: int) -> bool:
109+
# 递归验证从 start 位置开始的字符串是否满足累加数条件
110+
111+
# 如果已经到达字符串末尾,说明验证成功
112+
if start == len(num):
113+
return True
114+
115+
# 计算下一个数字的期望值
116+
expected_sum = a + b
117+
expected_str = str(expected_sum)
118+
119+
# 检查剩余字符串是否以期望的数字开头
120+
if num[start:].startswith(expected_str):
121+
# 递归验证下一个数字
122+
return self._isValid(num, start + len(expected_str), b, expected_sum)
123+
124+
return False
125+
```
126+
127+
### 思路 1:复杂度分析
128+
129+
- **时间复杂度**:$O(n^3)$,其中 $n$ 是字符串长度。外层双重循环枚举前两个数字的位置需要 $O(n^2)$ 时间,内层递归验证需要 $O(n)$ 时间,因此总时间复杂度为 $O(n^3)$。
130+
- **空间复杂度**:$O(n)$,递归调用栈的深度最多为 $O(n)$。

0 commit comments

Comments
 (0)