Skip to content

Commit 381ea94

Browse files
committed
二刷91
1 parent d91b170 commit 381ea94

File tree

8 files changed

+131
-27
lines changed

8 files changed

+131
-27
lines changed

docs/0091-decode-ways.adoc

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,96 @@
11
[#0091-decode-ways]
2-
= 91. Decode Ways
2+
= 91. 解码方法
33

4-
{leetcode}/problems/decode-ways/[LeetCode - Decode Ways^]
4+
https://leetcode.cn/problems/decode-ways/[LeetCode - 91. 解码方法 ^]
55

6-
这道题可以使用动态规划算法解决。学习《算法导论》中"动态规划"的解题步骤,再推演几次这个解题方法。
6+
一条包含字母 `A-Z` 的消息通过以下映射进行了 *编码*
77

8-
A message containing letters from `A-Z` is being encoded to numbers using the following mapping:
8+
`+"1" -> 'A'+` +
9+
`+"2" -> 'B'+` +
10+
`+...+` +
11+
`+"25" -> 'Y'+` +
12+
`+"26" -> 'Z'+`
913

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
'A' -> 1
13-
'B' -> 2
14-
...
15-
'Z' -> 26
14+
然而,在 *解码* 已编码的消息时,你意识到有许多不同的方式来解码,因为有些编码被包含在其它编码当中(`2``5``25`)。
1615

17-
----
16+
例如,`11106` 可以映射为:
1817

19-
Given a *non-empty* string containing only digits, determine the total number of ways to decode it.
18+
* `AAJF` ,将消息分组为 `+(1, 1, 10, 6)+`
19+
* `KJF` ,将消息分组为 `+(11, 10, 6)+`
20+
* 消息不能分组为 `+(1, 11, 06)+` ,因为 `06` 不是一个合法编码(只有
21+
"6" 是合法的)。
2022
21-
*Example 1:*
23+
注意,可能存在无法解码的字符串。
2224

23-
[subs="verbatim,quotes,macros"]
24-
----
25-
*Input:* "12"
26-
*Output:* 2
27-
*Explanation:* It could be decoded as "AB" (1 2) or "L" (12).
25+
给你一个只含数字的 *非空* 字符串 `s` ,请计算并返回 *解码* 方法的 *总数*。如果没有合法的方式解码整个字符串,返回 `0`
2826

29-
----
27+
题目数据保证答案肯定是一个 *32 位* 的整数。
3028

31-
*Example 2:*
29+
*示例 1:*
3230

33-
[subs="verbatim,quotes,macros"]
34-
----
35-
*Input:* "226"
36-
*Output:* 3
37-
*Explanation:* It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
38-
----
31+
....
32+
输入:s = "12"
33+
输出:2
34+
解释:它可以解码为 "AB"(1 2)或者 "L"(12)。
35+
....
36+
37+
*示例 2:*
38+
39+
....
40+
输入:s = "226"
41+
输出:3
42+
解释:它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
43+
....
44+
45+
*示例 3:*
46+
47+
....
48+
输入:s = "06"
49+
输出:0
50+
解释:"06" 无法映射到 "F" ,因为存在前导零("6" 和 "06" 并不等价)。
51+
....
52+
53+
*提示:*
54+
55+
* `+1 <= s.length <= 100+`
56+
* `s` 只包含数字,并且可能包含前导零。
3957
4058
59+
== 思路分析
60+
61+
image::images/0091-10.png[{image_attr}]
62+
63+
image::images/0091-11.png[{image_attr}]
64+
65+
image::images/0091-12.png[{image_attr}]
66+
67+
这道题可以使用动态规划算法解决。学习《算法导论》中"动态规划"的解题步骤,再推演几次这个解题方法。
68+
4169
[[src-0091]]
70+
[tabs]
71+
====
72+
一刷::
73+
+
74+
--
4275
[{java_src_attr}]
4376
----
4477
include::{sourcedir}/_0091_DecodeWays.java[tag=answer]
4578
----
79+
--
80+
81+
二刷::
82+
+
83+
--
84+
[{java_src_attr}]
85+
----
86+
include::{sourcedir}/_0091_DecodeWays_2.java[tag=answer]
87+
----
88+
--
89+
====
90+
91+
92+
== 参考资料
4693

94+
. https://leetcode.cn/problems/decode-ways/solutions/1052885/jie-ma-fang-fa-tu-jie-dp-zui-qing-xi-yi-97hng/[91. 解码方法 - 解码方法 | 图解DP | 最清晰易懂的代码^]
95+
. https://leetcode.cn/problems/decode-ways/solutions/730937/gong-shui-san-xie-gen-ju-shu-ju-fan-wei-ug3dd/[91. 解码方法 - 【宫水三叶】根据数据范围切换「递归」与「递推」^]
96+
. https://leetcode.cn/problems/decode-ways/solutions/734344/jie-ma-fang-fa-by-leetcode-solution-p8np/[91. 解码方法 - 官方题解^]

docs/images/0091-10.png

42.3 KB
Loading

docs/images/0091-11.png

29 KB
Loading

docs/images/0091-12.png

30.2 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,11 @@ endif::[]
743743
|{doc_base_url}/0083-remove-duplicates-from-sorted-list.adoc[题解]
744744
|✅ 链表操作
745745

746+
|{counter:codes2503}
747+
|{leetcode_base_url}/decode-ways/[91. 解码方法^]
748+
|{doc_base_url}/0091-decode-ways.adoc[题解]
749+
|❌ 动态规划。递推公式还要仔细推敲。
750+
746751
|===
747752

748753
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0091_DecodeWays.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ public class _0091_DecodeWays {
4545
* Memory Usage: 41.5 MB, less than 5.66% of Java online submissions for Decode Ways.
4646
*
4747
* Copy from: https://leetcode.com/problems/decode-ways/discuss/30357/DP-Solution-(Java)-for-reference[DP Solution (Java) for reference - LeetCode Discuss]
48+
*
49+
* @author D瓜哥 · https://www.diguage.com
50+
* @since 2020-01-18 00:20
4851
*/
4952
public int numDecodings(String s) {
50-
if (Objects.isNull(s) || s.length() == 0) {
53+
if (Objects.isNull(s) || s.isEmpty()) {
5154
return 0;
5255
}
5356
int length = s.length();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0091_DecodeWays_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-05-20 16:13:52
9+
*/
10+
public int numDecodings(String s) {
11+
if (s.charAt(0) == '0') {
12+
return 0;
13+
}
14+
int[] dp = new int[s.length() + 1];
15+
dp[0] = 1; // 没有字符也是一种解码方案(主要是为了保证后续递推的正确性)
16+
for (int i = 1; i <= s.length(); i++) {
17+
int val = s.charAt(i - 1) - '0';
18+
if (val != 0) {
19+
dp[i] = dp[i - 1];
20+
}
21+
if (i >= 2) {
22+
int pre = (s.charAt(i - 2) - '0') * 10;
23+
int sum = pre + val;
24+
if (sum >= 10 && sum <= 26) {
25+
dp[i] += dp[i - 2];
26+
}
27+
}
28+
}
29+
return dp[s.length()];
30+
}
31+
// end::answer[]
32+
public static void main(String[] args) {
33+
new _0091_DecodeWays_2().numDecodings("2101");
34+
}
35+
}

tools/d-format-problems-description.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ sed -i 's@`\([0-9a-zA-Z]\+\)`~`\([0-9a-zA-Z]\+\)`~@`\1~\2~@g' *.adoc
3030

3131
# +[0, 300]+
3232
sed -i 's@+\(\[[0-9]\+, *[0-9]\+\]\)+@\1@g' *.adoc
33+
34+
#+A-Z+
35+
sed -i 's@`+\([a-zA-Z0-9]\+ *- *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
36+
sed -i 's@`+\([a-zA-Z0-9]\+ *+ *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
37+
sed -i 's@`+\([a-zA-Z0-9]\+ *< *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
38+
sed -i 's@`+\([a-zA-Z0-9]\+ *> *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
39+
sed -i 's@`+\([a-zA-Z0-9]\+ *= *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
40+
sed -i 's@`+\([a-zA-Z0-9]\+ *== *[a-zA-Z0-9]\+\)+`@`\1`@g' *.adoc
41+
42+
#`"2"`
43+
sed -i 's@`+"\([a-zA-Z0-9]\+\)"+`@`\1`@g' *.adoc

0 commit comments

Comments
 (0)