639:解码方法 II

This commit is contained in:
huangge1199 2021-09-27 15:58:56 +08:00
parent 589766d6fe
commit 3318e9f62e
4 changed files with 193 additions and 4 deletions

View 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

View File

@ -0,0 +1,65 @@
<p>一条包含字母&nbsp;<code>A-Z</code> 的消息通过以下的方式进行了编码:</p>
<pre>
'A' -&gt; 1
'B' -&gt; 2
...
'Z' -&gt; 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>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 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