思路展示
示例代码
class Solution {
public:
int numDecodings(string s) {
int len=s.length();
vector<int> dp(len+1);
dp[0]=1;//空字符串的时候有一种方案
for(int i=1;i<=len;i++){
//一个单独字母的情况
//第一个位置对应的元素在字符串中的下标为0
if(s[i-1]!='0'){
dp[i]+=dp[i-1];
}
//两个字母的情况
if(i>1&&s[i-2]!='0'&&((s[i-2]-'0')*10+(s[i-1]-'0')<=26)){
dp[i]+=dp[i-2];
}
}
return dp[len];
}
};