Skip to content

Commit 3f2335b

Browse files
committed
更新题解列表
1 parent 261084e commit 3f2335b

File tree

9 files changed

+244
-21
lines changed

9 files changed

+244
-21
lines changed

Contents/00.Introduction/04.Solutions-List.md

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

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -566,6 +566,7 @@
566566
| 1710 | [卡车上的最大单元数](https://leetcode.cn/problems/maximum-units-on-a-truck/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1710.%20%E5%8D%A1%E8%BD%A6%E4%B8%8A%E7%9A%84%E6%9C%80%E5%A4%A7%E5%8D%95%E5%85%83%E6%95%B0.md) | 贪心、数组、排序 | 简单 |
567567
| 1720 | [解码异或后的数组](https://leetcode.cn/problems/decode-xored-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1720.%20%E8%A7%A3%E7%A0%81%E5%BC%82%E6%88%96%E5%90%8E%E7%9A%84%E6%95%B0%E7%BB%84.md) | 位运算、数组 | 简单 |
568568
| 1736 | [替换隐藏数字得到的最晚时间](https://leetcode.cn/problems/latest-time-by-replacing-hidden-digits/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1736.%20%E6%9B%BF%E6%8D%A2%E9%9A%90%E8%97%8F%E6%95%B0%E5%AD%97%E5%BE%97%E5%88%B0%E7%9A%84%E6%9C%80%E6%99%9A%E6%97%B6%E9%97%B4.md) | 贪心、字符串 | 简单 |
569+
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1742.%20%E7%9B%92%E5%AD%90%E4%B8%AD%E5%B0%8F%E7%90%83%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 哈希表、数学、计数 | 简单 |
569570
| 1779 | [找到最近的有相同 X 或 Y 坐标的点](https://leetcode.cn/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1779.%20%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E6%9C%89%E7%9B%B8%E5%90%8C%20X%20%E6%88%96%20Y%20%E5%9D%90%E6%A0%87%E7%9A%84%E7%82%B9.md) | 数组 | 简单 |
570571
| 1790 | [仅执行一次字符串交换能否使两个字符串相等](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1790.%20%E4%BB%85%E6%89%A7%E8%A1%8C%E4%B8%80%E6%AC%A1%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BA%A4%E6%8D%A2%E8%83%BD%E5%90%A6%E4%BD%BF%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E7%AD%89.md) | 哈希表、字符串、计数 | 简单 |
571572
| 1822 | [数组元素积的符号](https://leetcode.cn/problems/sign-of-the-product-of-an-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1822.%20%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%A7%AF%E7%9A%84%E7%AC%A6%E5%8F%B7.md) | 数组、数学 | 简单 |
@@ -788,6 +789,7 @@
788789
| 面试题 16.02 | [单词频率](https://leetcode.cn/problems/words-frequency-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2016.02.%20%E5%8D%95%E8%AF%8D%E9%A2%91%E7%8E%87.md) | 设计、字典树、数组、哈希表、字符串 | 中等 |
789790
| 面试题 16.05 | [阶乘尾数](https://leetcode.cn/problems/factorial-zeros-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2016.05.%20%E9%98%B6%E4%B9%98%E5%B0%BE%E6%95%B0.md) | 数学 | 简单 |
790791
| 面试题 16.26 | [计算器](https://leetcode.cn/problems/calculator-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2016.26.%20%E8%AE%A1%E7%AE%97%E5%99%A8.md) | 栈、数学、字符串 | 中等 |
792+
| 面试题 17.06 | [2出现的次数](https://leetcode.cn/problems/number-of-2s-in-range-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2017.06.%202%E5%87%BA%E7%8E%B0%E7%9A%84%E6%AC%A1%E6%95%B0.md) | 递归、数学、动态规划 | 困难 |
791793
| 面试题 17.14 | [最小K个数](https://leetcode.cn/problems/smallest-k-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2017.14.%20%E6%9C%80%E5%B0%8FK%E4%B8%AA%E6%95%B0.md) | 数组、分治、快速选择、排序、堆(优先队列) | 中等 |
792794
| 面试题 17.15 | [最长单词](https://leetcode.cn/problems/longest-word-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2017.15.%20%E6%9C%80%E9%95%BF%E5%8D%95%E8%AF%8D.md) | 字典树、数组、哈希表、字符串 | 中等 |
793795
| 面试题 17.17 | [多次搜索](https://leetcode.cn/problems/multi-search-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2017.17.%20%E5%A4%9A%E6%AC%A1%E6%90%9C%E7%B4%A2.md) | 字典树、数组、哈希表、字符串、字符串匹配、滑动窗口 | 中等 |

Contents/00.Introduction/05.Categories-List.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,8 @@
10731073
| 0248 | [中心对称数 III](https://leetcode.cn/problems/strobogrammatic-number-iii/) | | 递归、数组、字符串 | 困难 |
10741074
| 1088 | [易混淆数 II](https://leetcode.cn/problems/confusing-number-ii/) | | 数学、回溯 | 困难 |
10751075
| 1067 | [范围内的数字计数](https://leetcode.cn/problems/digit-count-in-range/) | | 数学、动态规划 | 困难 |
1076-
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | | 哈希表、数学、计数 | 简单 |
1077-
| 面试题 17.06 | [2出现的次数](https://leetcode.cn/problems/number-of-2s-in-range-lcci/) | | 递归、数学、动态规划 | 困难 |
1076+
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1742.%20%E7%9B%92%E5%AD%90%E4%B8%AD%E5%B0%8F%E7%90%83%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 哈希表、数学、计数 | 简单 |
1077+
| 面试题 17.06 | [2出现的次数](https://leetcode.cn/problems/number-of-2s-in-range-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2017.06.%202%E5%87%BA%E7%8E%B0%E7%9A%84%E6%AC%A1%E6%95%B0.md) | 递归、数学、动态规划 | 困难 |
10781078

10791079
### 概率 DP 题目
10801080

Contents/10.Dynamic-Programming/09.Digit-DP/02.Digit-DP-List.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
| 0248 | [中心对称数 III](https://leetcode.cn/problems/strobogrammatic-number-iii/) | | 递归、数组、字符串 | 困难 |
1414
| 1088 | [易混淆数 II](https://leetcode.cn/problems/confusing-number-ii/) | | 数学、回溯 | 困难 |
1515
| 1067 | [范围内的数字计数](https://leetcode.cn/problems/digit-count-in-range/) | | 数学、动态规划 | 困难 |
16-
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | | 哈希表、数学、计数 | 简单 |
17-
| 面试题 17.06 | [2出现的次数](https://leetcode.cn/problems/number-of-2s-in-range-lcci/) | | 递归、数学、动态规划 | 困难 |
16+
| 1742 | [盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1742.%20%E7%9B%92%E5%AD%90%E4%B8%AD%E5%B0%8F%E7%90%83%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E9%87%8F.md) | 哈希表、数学、计数 | 简单 |
17+
| 面试题 17.06 | [2出现的次数](https://leetcode.cn/problems/number-of-2s-in-range-lcci/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/%E9%9D%A2%E8%AF%95%E9%A2%98%2017.06.%202%E5%87%BA%E7%8E%B0%E7%9A%84%E6%AC%A1%E6%95%B0.md) | 递归、数学、动态规划 | 困难 |
1818

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,4 @@
259259
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)
260260

261261
## 11. 附加内容
262-
## [12. LeetCode 题解(已完成 789 道)](./Contents/00.Introduction/04.Solutions-List.md)
262+
## [12. LeetCode 题解(已完成 791 道)](./Contents/00.Introduction/04.Solutions-List.md)

Solutions/0233. 数字 1 的个数.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
3. 然后根据 $[minX, maxX]$ 来枚举能够填入的数字 $d$。
4949
4. 方案数累加上当前位选择 $d$ 之后的方案数,即:`ans += dfs(pos + 1, cnt + (d == 1), isLimit and d == maxX)`
5050
1. `cnt + (d == 1)` 表示之前数字 $1$ 出现的个数加上当前位为数字 $1$ 的个数。
51-
2. `isLimit and d == maxX` 表示 $pos + 1$ 位受到之前位限制和 $pos$ 位限制。
51+
2. `isLimit and d == maxX` 表示 $pos + 1$ 位受到之前位 $pos$ 位限制。
5252
6. 最后的方案数为 `dfs(0, 0, True)`,将其返回即可。
5353

5454
### 思路 1:代码
@@ -65,7 +65,6 @@ class Solution:
6565
# isLimit: 表示是否受到选择限制。如果为真,则第 pos 位填入数字最多为 s[pos];如果为假,则最大可为 9。
6666
def dfs(pos, cnt, isLimit):
6767
if pos == len(s):
68-
# isNum 为 True,则表示当前方案符合要求
6968
return cnt
7069

7170
ans = 0
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# [1742. 盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/)
2+
3+
- 标签:哈希表、数学、计数
4+
- 难度:简单
5+
6+
## 题目大意
7+
8+
**描述**:给定两个整数 $lowLimit$ 和 $highLimt$,代表 $n$ 个小球的编号(包括 $lowLimit$ 和 $highLimit$,即 $n == highLimit = lowLimit + 1$)。另外有无限个盒子。
9+
10+
现在的工作是将每个小球放入盒子中,其中盒子的编号应当等于小球编号上每位数字的和。例如,编号 $321$ 的小球应当放入编号 $3 + 2 + 1 = 6$ 的盒子,而编号 $10$ 的小球应当放入编号 $1 + 0 = 1$ 的盒子。
11+
12+
**要求**:返回放有最多小球的盒子中的小球数量。如果有多个盒子都满足放有最多小球,只需返回其中任一盒子的小球数量。
13+
14+
**说明**
15+
16+
- $1 \le lowLimit \le highLimit \le 10^5$。
17+
18+
**示例**
19+
20+
- 示例 1:
21+
22+
```Python
23+
输入:lowLimit = 1, highLimit = 10
24+
输出:2
25+
解释:
26+
盒子编号:1 2 3 4 5 6 7 8 9 10 11 ...
27+
小球数量:2 1 1 1 1 1 1 1 1 0 0 ...
28+
编号 1 的盒子放有最多小球,小球数量为 2
29+
```
30+
31+
- 示例 2:
32+
33+
```Python
34+
输入:lowLimit = 5, highLimit = 15
35+
输出:2
36+
解释:
37+
盒子编号:1 2 3 4 5 6 7 8 9 10 11 ...
38+
小球数量:1 1 1 1 2 2 1 1 1 0 0 ...
39+
编号 56 的盒子放有最多小球,每个盒子中的小球数量都是 2
40+
```
41+
42+
## 解题思路
43+
44+
### 思路 1:动态规划 + 数位 DP
45+
46+
将 $lowLimit$、$highLimit$ 转为字符串 $s1$、$s2$,并将 $s1$ 补上前导 $0$,令其与 $s2$ 长度一致。定义递归函数 `def dfs(pos, remainTotal, isMaxLimit, isMinLimit):` 表示构造第 $pos$ 位及之后剩余数位和为 $remainTotal$ 的合法方案数。
47+
48+
因为数据范围为 $[1, 10^5]$,对应数位和范围为 $[1, 45]$。因此我们可以枚举所有的数位和,并递归调用 `dfs(i, remainTotal, isMaxLimit, isMinLimit)`,求出不同数位和对应的方案数,并求出最大方案数。
49+
50+
接下来按照如下步骤进行递归。
51+
52+
1.`dfs(0, i, True, True)` 开始递归。 `dfs(0, i, True, True)` 表示:
53+
1. 从位置 $0$ 开始构造。
54+
2. 剩余数位和为 $i$。
55+
3. 开始时当前数位最大值受到最高位数位的约束。
56+
4. 开始时当前数位最小值受到最高位数位的约束。
57+
58+
2. 如果剩余数位和小于 $0$,说明当前方案不符合要求,则返回方案数 $0$。
59+
60+
3. 如果遇到 $pos == len(s)$,表示到达数位末尾,此时:
61+
1. 如果剩余数位和 $remainTotal$ 等于 $0$,说明当前方案符合要求,则返回方案数 $1$。
62+
2. 如果剩余数位和 $remainTotal$ 不等于 $0$,说明当前方案不符合要求,则返回方案数 $0$。
63+
64+
4. 如果 $pos \ne len(s)$,则定义方案数 $ans$,令其等于 $0$,即:`ans = 0`
65+
5. 如果遇到 $isNum == False$,说明之前位数没有填写数字,当前位可以跳过,这种情况下方案数等于 $pos + 1$ 位置上没有受到 $pos$ 位的约束,并且之前没有填写数字时的方案数,即:`ans = dfs(i + 1, state, False, False)`
66+
6. 根据 $isMaxLimit$ 和 $isMinLimit$ 来决定填当前位数位所能选择的最小数字($minX$)和所能选择的最大数字($maxX$)。
67+
68+
7. 然后根据 $[minX, maxX]$ 来枚举能够填入的数字 $d$。
69+
8. 方案数累加上当前位选择 $d$ 之后的方案数,即:`ans += dfs(pos + 1, remainTotal - d, isMaxLimit and d == maxX, isMinLimit and d == minX)`
70+
1. `remainTotal - d` 表示当前剩余数位和减去 $d$。
71+
2. `isMaxLimit and d == maxX` 表示 $pos + 1$ 位最大值受到之前 $pos$ 位限制。
72+
3. `isMinLimit and d == maxX` 表示 $pos + 1$ 位最小值受到之前 $pos$ 位限制。
73+
9. 最后返回所有 `dfs(0, i, True, True)` 中最大的方案数即可。
74+
75+
### 思路 1:代码
76+
77+
```Python
78+
class Solution:
79+
def countBalls(self, lowLimit: int, highLimit: int) -> int:
80+
s1, s2 = str(lowLimit), str(highLimit)
81+
82+
m, n = len(s1), len(s2)
83+
if m < n:
84+
s1 = '0' * (n - m) + s1
85+
86+
@cache
87+
# pos: 第 pos 个数位
88+
# remainTotal: 表示剩余数位和
89+
# isMaxLimit: 表示是否受到上限选择限制。如果为真,则第 pos 位填入数字最多为 s2[pos];如果为假,则最大可为 9。
90+
# isMinLimit: 表示是否受到下限选择限制。如果为真,则第 pos 位填入数字最小为 s1[pos];如果为假,则最小可为 0。
91+
def dfs(pos, remainTotal, isMaxLimit, isMinLimit):
92+
if remainTotal < 0:
93+
return 0
94+
if pos == n:
95+
# remainTotal 为 0,则表示当前方案符合要求
96+
return int(remainTotal == 0)
97+
98+
ans = 0
99+
# 如果前一位没有填写数字,或受到选择限制,则最小可选择数字为 s1[pos],否则最少为 0(可以含有前导 0)。
100+
minX = int(s1[pos]) if isMinLimit else 0
101+
# 如果受到选择限制,则最大可选择数字为 s[pos],否则最大可选择数字为 9。
102+
maxX = int(s2[pos]) if isMaxLimit else 9
103+
104+
# 枚举可选择的数字
105+
for d in range(minX, maxX + 1):
106+
ans += dfs(pos + 1, remainTotal - d, isMaxLimit and d == maxX, isMinLimit and d == minX)
107+
return ans
108+
109+
ans = 0
110+
for i in range(46):
111+
ans = max(ans, dfs(0, i, True, True))
112+
return ans
113+
```
114+
115+
### 思路 1:复杂度分析
116+
117+
- **时间复杂度**:$O(n \times \log n \times 45)$。
118+
- **空间复杂度**:$O(\log n)$。

Solutions/2376. 统计特殊整数.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,25 @@
3939
将 $n$ 转换为字符串 $s$,定义递归函数 `def dfs(pos, state, isLimit, isNum):` 表示构造第 $pos$ 位及之后所有数位的合法方案数。接下来按照如下步骤进行递归。
4040

4141
1.`dfs(0, 0, True, False)` 开始递归。 `dfs(0, 0, True, False)` 表示:
42-
1. 从位置 $0$ 开始构造。
43-
2. 初始没有使用数字(即前一位所选数字集合为 $0$)。
44-
3. 开始时受到数字 $n$ 对应最高位数位的约束。
45-
4. 开始时没有填写数字。
42+
1. 从位置 $0$ 开始构造。
43+
2. 初始没有使用数字(即前一位所选数字集合为 $0$)。
44+
3. 开始时受到数字 $n$ 对应最高位数位的约束。
45+
4. 开始时没有填写数字。
46+
4647
2. 如果遇到 $pos == len(s)$,表示到达数位末尾,此时:
47-
1. 如果 $isNum == True$,说明当前方案符合要求,则返回方案数 $1$。
48-
2. 如果 $isNum == False$,说明当前方案不符合要求,则返回方案数 $0$。
48+
1. 如果 $isNum == True$,说明当前方案符合要求,则返回方案数 $1$。
49+
2. 如果 $isNum == False$,说明当前方案不符合要求,则返回方案数 $0$。
50+
4951
3. 如果 $pos \ne len(s)$,则定义方案数 $ans$,令其等于 $0$,即:`ans = 0`
5052
4. 如果遇到 $isNum == False$,说明之前位数没有填写数字,当前位可以跳过,这种情况下方案数等于 $pos + 1$ 位置上没有受到 $pos$ 位的约束,并且之前没有填写数字时的方案数,即:`ans = dfs(i + 1, state, False, False)`
5153
5. 如果 $isNum == True$,则当前位必须填写一个数字。此时:
52-
1. 根据 $isNum$ 和 $isLimit$ 来决定填当前位数位所能选择的最小数字($minX$)和所能选择的最大数字($maxX$),
53-
2. 然后根据 $[minX, maxX]$ 来枚举能够填入的数字 $d$。
54-
3. 如果之前没有选择 $d$,即 $d$ 不在之前选择的数字集合 $state$ 中,则方案数累加上当前位选择 $d$ 之后的方案数,即:`ans += dfs(pos + 1, state | (1 << d), isLimit and d == maxX, True)`
55-
1. `state | (1 << d)` 表示之前选择的数字集合 $state$ 加上 $d$。
56-
2. `isLimit and d == maxX` 表示 $pos + 1$ 位受到之前位限制和 $pos$ 位限制。
57-
3. $isNum == True$ 表示 $pos$ 位选择了数字。
54+
1. 根据 $isNum$ 和 $isLimit$ 来决定填当前位数位所能选择的最小数字($minX$)和所能选择的最大数字($maxX$),
55+
2. 然后根据 $[minX, maxX]$ 来枚举能够填入的数字 $d$。
56+
3. 如果之前没有选择 $d$,即 $d$ 不在之前选择的数字集合 $state$ 中,则方案数累加上当前位选择 $d$ 之后的方案数,即:`ans += dfs(pos + 1, state | (1 << d), isLimit and d == maxX, True)`
57+
1. `state | (1 << d)` 表示之前选择的数字集合 $state$ 加上 $d$。
58+
2. `isLimit and d == maxX` 表示 $pos + 1$ 位受到之前 $pos$ 位限制。
59+
3. $isNum == True$ 表示 $pos$ 位选择了数字。
60+
5861
6. 最后的方案数为 `dfs(0, 0, True, False)`,将其返回即可。
5962

6063
### 思路 1:代码

Solutions/2719. 统计整数数目.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,33 @@
4141

4242
### 思路 1:动态规划 + 数位 DP
4343

44+
将 $num1$ 补上前导 $0$,补到和 $num2$ 长度一致,定义递归函数 `def dfs(pos, total, isMaxLimit, isMinLimit):` 表示构造第 $pos$ 位及之后所有数位的合法方案数。接下来按照如下步骤进行递归。
45+
46+
1.`dfs(0, 0, True, True)` 开始递归。 `dfs(0, 0, True, True)` 表示:
47+
1. 从位置 $0$ 开始构造。
48+
2. 初始数位和为 $0$。
49+
3. 开始时当前数位最大值受到最高位数位的约束。
50+
4. 开始时当前数位最小值受到最高位数位的约束。
51+
2. 如果 $total > max\underline{}sum$,说明当前方案不符合要求,则返回方案数 $0$。
52+
3. 如果遇到 $pos == len(s)$,表示到达数位末尾,此时:
53+
1. 如果 $min\underline{}sum \le total \le max\underline{}sum$,说明当前方案符合要求,则返回方案数 $1$。
54+
2. 如果不满足,则当前方案不符合要求,则返回方案数 $0$。
55+
4. 如果 $pos \ne len(s)$,则定义方案数 $ans$,令其等于 $0$,即:`ans = 0`
56+
5. 根据 $isMaxLimit$ 和 $isMinLimit$ 来决定填当前位数位所能选择的最小数字($minX$)和所能选择的最大数字($maxX$)。
57+
6. 然后根据 $[minX, maxX]$ 来枚举能够填入的数字 $d$。
58+
7. 方案数累加上当前位选择 $d$ 之后的方案数,即:`ans += dfs(pos + 1, total + d, isMaxLimit and d == maxX, isMinLimit and d == minX)`
59+
1. `total + d` 表示当前数位和 $total$ 加上 $d$。
60+
2. `isMaxLimit and d == maxX` 表示 $pos + 1$ 位最大值受到之前 $pos$ 位限制。
61+
3. `isMinLimit and d == maxX` 表示 $pos + 1$ 位最小值受到之前 $pos$ 位限制。
62+
8. 最后的方案数为 `dfs(0, 0, True, True) % MOD`,将其返回即可。
63+
4464
### 思路 1:代码
4565

4666
```Python
4767
class Solution:
4868
def count(self, num1: str, num2: str, min_sum: int, max_sum: int) -> int:
4969
MOD = 10 ** 9 + 7
50-
#n 转换为字符串 s
70+
#num1 补上前导 0,补到和 num2 长度一致
5171
m, n = len(num1), len(num2)
5272
if m < n:
5373
num1 = '0' * (n - m) + num1

0 commit comments

Comments
 (0)