639:解码方法 II
This commit is contained in:
parent
589766d6fe
commit
3318e9f62e
126
src/main/java/leetcode/editor/cn/DecodeWaysIi.java
Normal file
126
src/main/java/leetcode/editor/cn/DecodeWaysIi.java
Normal file
@ -0,0 +1,126 @@
|
||||
//一条包含字母 A-Z 的消息通过以下的方式进行了编码:
|
||||
//
|
||||
//
|
||||
//'A' -> 1
|
||||
//'B' -> 2
|
||||
//...
|
||||
//'Z' -> 26
|
||||
//
|
||||
//
|
||||
// 要 解码 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,"11106" 可以映射为:
|
||||
//
|
||||
//
|
||||
// "AAJF" 对应分组 (1 1 10 6)
|
||||
// "KJF" 对应分组 (11 10 6)
|
||||
//
|
||||
//
|
||||
// 注意,像 (1 11 06) 这样的分组是无效的,因为 "06" 不可以映射为 'F' ,因为 "6" 与 "06" 不同。
|
||||
//
|
||||
// 除了 上面描述的数字字母映射方案,编码消息中可能包含 '*' 字符,可以表示从 '1' 到 '9' 的任一数字(不包括 '0')。例如,编码字符串 "1*
|
||||
//" 可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条消息。对 "1*" 进行解码,相当于解码
|
||||
//该字符串可以表示的任何编码消息。
|
||||
//
|
||||
// 给你一个字符串 s ,由数字和 '*' 字符组成,返回 解码 该字符串的方法 数目 。
|
||||
//
|
||||
// 由于答案数目可能非常大,返回对 10⁹ + 7 取余 的结果。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:s = "*"
|
||||
//输出:9
|
||||
//解释:这一条编码消息可以表示 "1"、"2"、"3"、"4"、"5"、"6"、"7"、"8" 或 "9" 中的任意一条。
|
||||
//可以分别解码成字符串 "A"、"B"、"C"、"D"、"E"、"F"、"G"、"H" 和 "I" 。
|
||||
//因此,"*" 总共有 9 种解码方法。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:s = "1*"
|
||||
//输出:18
|
||||
//解释:这一条编码消息可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条。
|
||||
//每种消息都可以由 2 种方法解码(例如,"11" 可以解码成 "AA" 或 "K")。
|
||||
//因此,"1*" 共有 9 * 2 = 18 种解码方法。
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:s = "2*"
|
||||
//输出:15
|
||||
//解释:这一条编码消息可以表示 "21"、"22"、"23"、"24"、"25"、"26"、"27"、"28" 或 "29" 中的任意一条。
|
||||
//"21"、"22"、"23"、"24"、"25" 和 "26" 由 2 种解码方法,但 "27"、"28" 和 "29" 仅有 1 种解码方法。
|
||||
//因此,"2*" 共有 (6 * 2) + (3 * 1) = 12 + 3 = 15 种解码方法。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= s.length <= 10⁵
|
||||
// s[i] 是 0 - 9 中的一位数字或字符 '*'
|
||||
//
|
||||
// Related Topics 字符串 动态规划 👍 137 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//639:解码方法 II
|
||||
class DecodeWaysIi {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new DecodeWaysIi().new Solution();
|
||||
solution.numDecodings("*1*1*0");
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
static final int MOD = 1000000007;
|
||||
|
||||
public int numDecodings(String s) {
|
||||
int n = s.length();
|
||||
long a = 0, b = 1, c = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
c = b * check1digit(s.charAt(i - 1)) % MOD;
|
||||
if (i > 1) {
|
||||
c = (c + a * check2digits(s.charAt(i - 2), s.charAt(i - 1))) % MOD;
|
||||
}
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
return (int) c;
|
||||
}
|
||||
|
||||
public int check1digit(char ch) {
|
||||
if (ch == '0') {
|
||||
return 0;
|
||||
}
|
||||
return ch == '*' ? 9 : 1;
|
||||
}
|
||||
|
||||
public int check2digits(char c0, char c1) {
|
||||
if (c0 == '*' && c1 == '*') {
|
||||
return 15;
|
||||
}
|
||||
if (c0 == '*') {
|
||||
return c1 <= '6' ? 2 : 1;
|
||||
}
|
||||
if (c1 == '*') {
|
||||
if (c0 == '1') {
|
||||
return 9;
|
||||
}
|
||||
if (c0 == '2') {
|
||||
return 6;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return (c0 != '0' && (c0 - '0') * 10 + (c1 - '0') <= 26) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
File diff suppressed because one or more lines are too long
65
src/main/java/leetcode/editor/cn/doc/content/DecodeWaysIi.md
Normal file
65
src/main/java/leetcode/editor/cn/doc/content/DecodeWaysIi.md
Normal file
@ -0,0 +1,65 @@
|
||||
<p>一条包含字母 <code>A-Z</code> 的消息通过以下的方式进行了编码:</p>
|
||||
|
||||
<pre>
|
||||
'A' -> 1
|
||||
'B' -> 2
|
||||
...
|
||||
'Z' -> 26
|
||||
</pre>
|
||||
|
||||
<p>要 <strong>解码</strong> 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,<code>"11106"</code> 可以映射为:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>"AAJF"</code> 对应分组 <code>(1 1 10 6)</code></li>
|
||||
<li><code>"KJF"</code> 对应分组 <code>(11 10 6)</code></li>
|
||||
</ul>
|
||||
|
||||
<p>注意,像 <code>(1 11 06)</code> 这样的分组是无效的,因为 <code>"06"</code> 不可以映射为 <code>'F'</code> ,因为 <code>"6"</code> 与 <code>"06"</code> 不同。</p>
|
||||
|
||||
<p><strong>除了</strong> 上面描述的数字字母映射方案,编码消息中可能包含 <code>'*'</code> 字符,可以表示从 <code>'1'</code> 到 <code>'9'</code> 的任一数字(不包括 <code>'0'</code>)。例如,编码字符串 <code>"1*"</code> 可以表示 <code>"11"</code>、<code>"12"</code>、<code>"13"</code>、<code>"14"</code>、<code>"15"</code>、<code>"16"</code>、<code>"17"</code>、<code>"18"</code> 或 <code>"19"</code> 中的任意一条消息。对 <code>"1*"</code> 进行解码,相当于解码该字符串可以表示的任何编码消息。</p>
|
||||
|
||||
<p>给你一个字符串 <code>s</code> ,由数字和 <code>'*'</code> 字符组成,返回 <strong>解码</strong> 该字符串的方法 <strong>数目</strong> 。</p>
|
||||
|
||||
<p>由于答案数目可能非常大,返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 的结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "*"
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>这一条编码消息可以表示 "1"、"2"、"3"、"4"、"5"、"6"、"7"、"8" 或 "9" 中的任意一条。
|
||||
可以分别解码成字符串 "A"、"B"、"C"、"D"、"E"、"F"、"G"、"H" 和 "I" 。
|
||||
因此,"*" 总共有 9 种解码方法。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "1*"
|
||||
<strong>输出:</strong>18
|
||||
<strong>解释:</strong>这一条编码消息可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条。
|
||||
每种消息都可以由 2 种方法解码(例如,"11" 可以解码成 "AA" 或 "K")。
|
||||
因此,"1*" 共有 9 * 2 = 18 种解码方法。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "2*"
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>这一条编码消息可以表示 "21"、"22"、"23"、"24"、"25"、"26"、"27"、"28" 或 "29" 中的任意一条。
|
||||
"21"、"22"、"23"、"24"、"25" 和 "26" 由 2 种解码方法,但 "27"、"28" 和 "29" 仅有 1 种解码方法。
|
||||
因此,"2*" 共有 (6 * 2) + (3 * 1) = 12 + 3 = 15 种解码方法。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 是 <code>0 - 9</code> 中的一位数字或字符 <code>'*'</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>字符串</li><li>动态规划</li></div></div><br><div><li>👍 137</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user