Skip to content

Commit bfedda1

Browse files
committed
补充第 400 ~ 499 题的题目解析(增加 14 道题)
1 parent 4e06208 commit bfedda1

17 files changed

+1720
-1
lines changed

docs/00_preface/00_05_solutions_list.md

Lines changed: 15 additions & 1 deletion
Large diffs are not rendered by default.

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-28 补充第 400 ~ 499 题的题目解析(增加 14 道题)
34
- 2025-10-27 补充第 400 ~ 499 题的题目解析(增加 15 道题)
45
- 2025-10-24 补充第 400 ~ 499 题的题目解析(增加 12 道题)
56
- 2025-10-23 补全第 300 ~ 399 题的题目解析(增加 20 道题)

docs/solutions/0400-0499/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
- [0400. 第 N 位数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/nth-digit.md)
44
- [0401. 二进制手表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/binary-watch.md)
5+
- [0402. 移掉 K 位数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/remove-k-digits.md)
56
- [0403. 青蛙过河](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/frog-jump.md)
67
- [0404. 左叶子之和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/sum-of-left-leaves.md)
78
- [0405. 数字转换为十六进制数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/convert-a-number-to-hexadecimal.md)
89
- [0406. 根据身高重建队列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/queue-reconstruction-by-height.md)
10+
- [0407. 接雨水 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/trapping-rain-water-ii.md)
911
- [0409. 最长回文串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/longest-palindrome.md)
1012
- [0410. 分割数组的最大值](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/split-array-largest-sum.md)
1113
- [0412. Fizz Buzz](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/fizz-buzz.md)
1214
- [0413. 等差数列划分](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/arithmetic-slices.md)
15+
- [0414. 第三大的数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/third-maximum-number.md)
1316
- [0415. 字符串相加](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/add-strings.md)
1417
- [0416. 分割等和子集](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/partition-equal-subset-sum.md)
1518
- [0417. 太平洋大西洋水流问题](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/pacific-atlantic-water-flow.md)
1619
- [0419. 棋盘上的战舰](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/battleships-in-a-board.md)
20+
- [0420. 强密码检验器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/strong-password-checker.md)
1721
- [0421. 数组中两个数的最大异或值](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/maximum-xor-of-two-numbers-in-an-array.md)
22+
- [0423. 从英文中重建数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/reconstruct-original-digits-from-english.md)
1823
- [0424. 替换后的最长重复字符](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/longest-repeating-character-replacement.md)
1924
- [0425. 单词方块](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/word-squares.md)
2025
- [0426. 将二叉搜索树转化为排序的双向链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/convert-binary-search-tree-to-sorted-doubly-linked-list.md)
@@ -24,6 +29,7 @@
2429
- [0430. 扁平化多级双向链表](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/flatten-a-multilevel-doubly-linked-list.md)
2530
- [0432. 全 O(1) 的数据结构](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/all-oone-data-structure.md)
2631
- [0433. 最小基因变化](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/minimum-genetic-mutation.md)
32+
- [0434. 字符串中的单词数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/number-of-segments-in-a-string.md)
2733
- [0435. 无重叠区间](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/non-overlapping-intervals.md)
2834
- [0436. 寻找右区间](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/find-right-interval.md)
2935
- [0437. 路径总和 III](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/path-sum-iii.md)
@@ -36,6 +42,7 @@
3642
- [0446. 等差数列划分 II - 子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/arithmetic-slices-ii-subsequence.md)
3743
- [0447. 回旋镖的数量](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/number-of-boomerangs.md)
3844
- [0448. 找到所有数组中消失的数字](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/find-all-numbers-disappeared-in-an-array.md)
45+
- [0449. 序列化和反序列化二叉搜索树](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/serialize-and-deserialize-bst.md)
3946
- [0450. 删除二叉搜索树中的节点](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/delete-node-in-a-bst.md)
4047
- [0451. 根据字符出现频率排序](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/sort-characters-by-frequency.md)
4148
- [0452. 用最少数量的箭引爆气球](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/minimum-number-of-arrows-to-burst-balloons.md)
@@ -44,6 +51,7 @@
4451
- [0455. 分发饼干](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/assign-cookies.md)
4552
- [0456. 132 模式](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/132-pattern.md)
4653
- [0457. 环形数组是否存在循环](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/circular-array-loop.md)
54+
- [0458. 可怜的小猪](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/poor-pigs.md)
4755
- [0459. 重复的子字符串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/repeated-substring-pattern.md)
4856
- [0460. LFU 缓存](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/lfu-cache.md)
4957
- [0461. 汉明距离](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/hamming-distance.md)
@@ -59,16 +67,22 @@
5967
- [0474. 一和零](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/ones-and-zeroes.md)
6068
- [0475. 供暖器](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/heaters.md)
6169
- [0476. 数字的补数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/number-complement.md)
70+
- [0477. 汉明距离总和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/total-hamming-distance.md)
6271
- [0478. 在圆内随机生成点](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/generate-random-point-in-a-circle.md)
6372
- [0479. 最大回文数乘积](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/largest-palindrome-product.md)
6473
- [0480. 滑动窗口中位数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/sliding-window-median.md)
6574
- [0481. 神奇字符串](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/magical-string.md)
6675
- [0482. 密钥格式化](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/license-key-formatting.md)
76+
- [0483. 最小好进制](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/smallest-good-base.md)
6777
- [0485. 最大连续 1 的个数](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/max-consecutive-ones.md)
6878
- [0486. 预测赢家](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/predict-the-winner.md)
6979
- [0487. 最大连续1的个数 II](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/max-consecutive-ones-ii.md)
80+
- [0488. 祖玛游戏](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/zuma-game.md)
7081
- [0491. 非递减子序列](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/non-decreasing-subsequences.md)
7182
- [0492. 构造矩形](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/construct-the-rectangle.md)
83+
- [0493. 翻转对](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/reverse-pairs.md)
7284
- [0494. 目标和](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/target-sum.md)
85+
- [0495. 提莫攻击](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/teemo-attacking.md)
7386
- [0496. 下一个更大元素 I](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/next-greater-element-i.md)
87+
- [0497. 非重叠矩形中的随机点](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/random-point-in-non-overlapping-rectangles.md)
7488
- [0498. 对角线遍历](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0400-0499/diagonal-traverse.md)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [0434. 字符串中的单词数](https://leetcode.cn/problems/number-of-segments-in-a-string/)
2+
3+
- 标签:字符串
4+
- 难度:简单
5+
6+
## 题目链接
7+
8+
- [0434. 字符串中的单词数 - 力扣](https://leetcode.cn/problems/number-of-segments-in-a-string/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
给定字符串 $s$。
15+
16+
**要求**
17+
18+
统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
19+
20+
**说明**
21+
22+
- 可以假定字符串里不包括任何不可打印的字符。
23+
24+
**示例**
25+
26+
- 示例 1:
27+
28+
```python
29+
输入: "Hello, my name is John"
30+
输出: 5
31+
解释: 这里的单词是指连续的不是空格的字符,所以 "Hello," 算作 1 个单词。
32+
```
33+
34+
## 解题思路
35+
36+
### 思路 1:遍历计数
37+
38+
思路非常简单,直接遍历统计单词数即可。
39+
40+
对于字符串 $s$ 来说,我们使用两个变量:
41+
42+
- $ans$:用来统计单词个数
43+
- $prev$:用来记录上一个字符是否为空格
44+
45+
然后遍历字符串:
46+
47+
- 如果当前字符不是空格,且上一个字符是空格(或者是第一个字符),则说明遇到了一个新的单词,单词数 $ans$ 加 $1$
48+
- 最后返回 $ans$ 即可。
49+
50+
### 思路 1:代码
51+
52+
```python
53+
class Solution:
54+
def countSegments(self, s: str) -> int:
55+
# ans 统计单词数,prev 记录上一个字符是否为空格
56+
ans = 0
57+
prev = True
58+
59+
# 遍历字符串
60+
for ch in s:
61+
# 如果当前字符不是空格,且上一个字符是空格,则遇到新单词
62+
if ch != ' ' and prev:
63+
ans += 1
64+
# 更新 prev:当前字符是否为空格
65+
prev = (ch == ' ')
66+
67+
return ans
68+
```
69+
70+
### 思路 1:复杂度分析
71+
72+
- **时间复杂度**:$O(n)$。其中 $n$ 是字符串 $s$ 的长度。
73+
- **空间复杂度**:$O(1)$。
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# [0458. 可怜的小猪](https://leetcode.cn/problems/poor-pigs/)
2+
3+
- 标签:数学、动态规划、组合数学
4+
- 难度:困难
5+
6+
## 题目链接
7+
8+
- [0458. 可怜的小猪 - 力扣](https://leetcode.cn/problems/poor-pigs/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
有 $buckets$ 桶液体,其中「正好有一桶」含有毒药,其余装的都是水。它们从外观看起来都一样。为了弄清楚哪只水桶含有毒药,你可以喂一些猪喝,通过观察猪是否会死进行判断。不幸的是,你只有 $minutesToTest$ 分钟时间来确定哪桶液体是有毒的。
15+
16+
喂猪的规则如下:
17+
18+
1. 选择若干活猪进行喂养。
19+
2. 可以允许小猪同时饮用任意数量的桶中的水,并且该过程不需要时间。
20+
3. 小猪喝完水后,必须有 $minutesToDie$ 分钟的冷却时间。在这段时间里,你只能观察,而不允许继续喂猪。
21+
4. 过了 $minutesToDie$ 分钟后,所有喝到毒药的猪都会死去,其他所有猪都会活下来。
22+
5. 重复这一过程,直到时间用完。
23+
24+
给定桶的数目 $buckets$,$minutesToDie$ 和 $minutesToTest$。
25+
26+
**要求**
27+
28+
返回在规定时间内判断哪个桶有毒所需的「最小」猪数。
29+
30+
**说明**
31+
32+
- $1 \le buckets \le 10^{3}$。
33+
- $1 \le minutesToDie \le minutesToTest \le 10^{3}$。
34+
35+
**示例**
36+
37+
- 示例 1:
38+
39+
```python
40+
输入:buckets = 1000, minutesToDie = 15, minutesToTest = 60
41+
输出:5
42+
```
43+
44+
- 示例 2:
45+
46+
```python
47+
输入:buckets = 4, minutesToDie = 15, minutesToTest = 15
48+
输出:2
49+
```
50+
51+
## 解题思路
52+
53+
### 思路 1:数学
54+
55+
这是一道经典的数学和信息论问题。
56+
57+
假设有 $x$ 只小猪,每只小猪可以进行 $rounds = \lfloor \frac{minutesToTest}{minutesToDie} \rfloor + 1$ 轮测试(第一轮 + 冷却后继续测试)。
58+
59+
每只小猪有 $rounds + 1$ 种状态:
60+
- 第 0 轮死亡(喝了第 0 次被毒死)。
61+
- 第 1 轮死亡(喝了第 1 次被毒死)。
62+
- ...
63+
- 第 $rounds - 1$ 轮死亡。
64+
- 存活到最后(没有中毒)。
65+
66+
$x$ 只小猪的状态组合总数为 $rounds^x$,每一种状态组合可以对应一个桶。
67+
68+
因此问题转化为:需要找到最小的 $x$,使得 $rounds^x \ge buckets$。
69+
70+
对不等式两边取对数:$x \times \log(rounds) \ge \log(buckets)$。
71+
72+
解得:$x \ge \frac{\log(buckets)}{\log(rounds)}$。
73+
74+
因此最小的 $x$ 为 $\lceil \frac{\log(buckets)}{\log(rounds)} \rceil$。
75+
76+
具体步骤:
77+
78+
- 计算 $rounds = \lfloor \frac{minutesToTest}{minutesToDie} \rfloor + 1$(即每只小猪可能的死亡状态数)。
79+
- 如果 $rounds = 1$,说明无法进行有效测试,需要小猪数等于桶数。
80+
- 否则计算 $\frac{\log(buckets)}{\log(rounds)}$。
81+
- 由于浮点数精度问题,如果结果接近整数,直接返回该整数,否则向上取整。
82+
83+
### 思路 1:代码
84+
85+
```python
86+
import math
87+
88+
class Solution:
89+
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
90+
# rounds 表示每只小猪可能的死亡状态数
91+
rounds = minutesToTest // minutesToDie + 1
92+
93+
# 特殊情况:如果只有 1 个状态(即无法测试),需要小猪数等于桶数
94+
if rounds == 1:
95+
return buckets
96+
97+
# 使用换底公式:log_rounds(buckets) = log(buckets) / log(rounds)
98+
# 添加小的 epsilon 避免浮点数精度问题
99+
result = math.log(buckets) / math.log(rounds)
100+
101+
# 如果 result 是整数(考虑浮点误差),直接返回,否则向上取整
102+
if abs(result - round(result)) < 1e-10:
103+
return round(result)
104+
else:
105+
return math.ceil(result)
106+
```
107+
108+
### 思路 1:复杂度分析
109+
110+
- **时间复杂度**:$O(1)$。只进行常数次计算。
111+
- **空间复杂度**:$O(1)$。只使用了常数个额外变量。

0 commit comments

Comments
 (0)