题目
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message “12”, it could be decoded as “AB” (1 2) or “L” (12).
The number of ways decoding “12” is 2.
分析
这道题真是做的失败,竟然提交了5次才AC,一下拉低了AC率一大截,真是气煞~~~
究其原因还是判断条件考虑的不全面,在判断过程中不仅需要判断输入字符串的合法性(特别是当前字符为‘0’的时候)又要将字符串长度为1,2时单独处理~
不想说了,失落的贴上并不优美的代码,懒得修改~~~
AC代码
class Solution {
public:
int numDecodings(string s) {
if (s.empty())
return 0;
//求字符串长度
int len = s.length();
//记录对应长度字符串有几种表示方式
vector<int> ways(len + 1, 0);
for (int i = 0; i < len; ++i)
{
//对首位字符
if (i == 0)
{
//满足[1,9]
if ((s[0] - '0') > 0 && (s[0] - '0') <= 9)
{
ways[i] = 1;
continue;
}
else
return 0;
}
else{
//得到前一位
int tmp1 = s[i - 1] - '0';
//得到当前位
int tmp2 = s[i] - '0';
int tmp = tmp1 * 10 + tmp2;
//如果该两个字符可以表示为一个字母
if (tmp >= 10 && tmp <= 26)
{
//且当前处理为下标第2或以上字符
if (i > 1)
{
//当前位为0
if (tmp2 == 0)
ways[i] = ways[i - 2];
else
ways[i] = ways[i - 1] + ways[i - 2];
}
//此时处理为下标为0,1的两个字符
else
{
if (tmp2 == 0)
ways[i] = 1;
else
ways[i] = 2;
}
}
else{
if ((s[i] - '0') > 0 && (s[i] - '0') <= 9)
ways[i] = ways[i - 1];
else{
//此时代表字符串中间嵌入非法0,表示方式为0
return 0;
}
}
}
}//for
return ways[len-1];
}
};