问题描述:
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.
求解码方案数。
问题求解:
class Solution {
public:
int numDecodings(string s) {
int n=s.size();
if(n==0 || s[0]=='0') return 0;
vector<int> ways(n+1);
ways[0]=1;
ways[1]=1;
for(int i=2;i<=n;i++)
{
if(s[i-1]!='0')
{//(1)满足一步的情况
//若s[i-1]不为0,则s[i-1]可单独存在,s[i]可由s[i-1]走1步到达
ways[i] = ways[i-1];
}
//(2)满足两步的情况
//承接上面,若s[i-2]*10+s[i-1]<=26,则s[i]可由s[i-2]走2步到达
//if(0<stoi(s.substr(i-2,2))<=26) //错误!!!
if(s[i-2]=='1' || (s[i-2]=='2'&&s[i-1]<='6'))
{
ways[i] += ways[i-2];//因与前面是先后顺序,所以是+=
}
}
return ways[n];
}
};
本文介绍了一种算法,用于计算给定数字字符串的不同字母解码方式的数量。通过动态规划方法实现,考虑了单字符和双字符两种解码情况。
1160

被折叠的 条评论
为什么被折叠?



