题目:
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: “12”
Output: 2
Explanation: It could be decoded as “AB” (1 2) or “L” (12).
Example 2:
Input: “226”
Output: 3
Explanation: It could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6).
解答:
这是一道动态规划的题,可惜开始拿到题目时并没有准确的思路。
假设给定字符串“1226”,则
当前位置 编码方式
[0] 1
[1] 1-2 12
[2] 1-2-2 12-2 1-22
[3] 1-2-2-6 12-2-6 1-22-6 1-2-26 12-26
由上述我们可以看出,假如在位置[3]时,可能的情况为两种:
一种是在位置[2]的所有情况中,后面均加上字符‘6’
第二种情况是在位置[1]的所有情况中,后面均加上‘26’
因此可以分析得出,在考虑第 i 个加进来的字符时:(用dp[i]表示前i个字符的解码方式)
- 若当前字符不为0,自己可以构成一个单独信息,则 dp[i] = dp[i-1]
- 若当前字符可与上一字符进行合并,构成有效信息,即上一字符不为0,且合并后得到的数字 <= 26,则 dp[i] = dp[i-2]
- 若上述两种情况均满足,则 dp[i]=dp[i-1]+dp[i-2]
代码实现如下:
class Solution {
public int numDecodings(String s) {
if(s.length() == 0) {
return 0;
}
int[] dp = new int[s.length() + 1];
//防止之后遍历时,i=1时dp[i-1]越界。dp[i]恰好表示第i个字符的编码方式
dp[0] = 1;
if(s.charAt(0) == '0') {
return 0;
} else {
dp[1] = 1;
}
for(int i=1; i<s.length(); i++) {
//若当前字符不为0,则可单独构成有效信息
if(s.charAt(i) != '0' ) {
dp[i+1] += dp [i];
}
int twoDigits = (s.charAt(i-1)-'0')*10 + (s.charAt(i)-'0');
//注意此处要判断上一位不为0的情况,否则会出现03这样的无效信息
if(s.charAt(i-1) != '0' && twoDigits <= 26) {
dp[i+1] += dp[i-1];
}
}
return dp[s.length()];
}
}