题目:
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).
描述:
定义字母A-Z分别为1-26,现给出一个数字序列,问该序列最多能转化成多少种不同的字母序列。
分析:
简单推导可知,最优情况下,不同的字母序列数为斐波那契数列值,即每个数字既能与前后一个数字组成1-26的数,本身又是1-9的数,dp[i + 1] = dp[i - 1] + dp[i]
另外就是对特殊情况进行处理,
数字开头是0,则输出0,
若当前位为0,加上前一位后组成的数字超过了26,则输出0
否则,dp[i + 1] = dp[i - 1]
若当前位不为0,但是最后两位数值超出了26,则dp[i + 1] = dp[i]
具体可以自行推导归纳
样例:
"9999999101111112101"
"999999910111111210"
"12120"
"11111111"
"111111110"
"110"
"130"
"1301"
"100"
"0"
"00000"
"101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010"
"10"
"101"
"1010"
"10101"
"216"
"266"
"1"
"11"
"111"
"1111"
"11111"
"111111"
"111116"
"111118"
"111129"
"12"
"129"
"1291"
"12912"
"129129"
代码:(时间复杂度O(n))
class Solution {
public:
int numDecodings(string s) {
if (s[0] == '0') {
return 0;
}
vector<int> num;
num.push_back(s[0] == '0' ? 0 : 1);
num.push_back(s[0] == '0' ? 0 : 1);
for (int i = 1; i < s.size(); ++ i) {
if (s[i - 1] == 1 + '0' && s[i] != '0' || s[i - 1] == 2 + '0' && s[i] != '0' && s[i] <= 6 + '0') {
num.push_back(num[i - 1] + num[i]);
} else {
if (s[i] == '0') {
if (s[i - 1] == '0' || s[i - 1] > '0' + 2) {
return 0;
} else {
num.push_back(num[i - 1]);
}
} else {
num.push_back(num[i]);
}
}
}
return num[s.size()];
}
};