dp一下就好了
const int MAXN = 1e6 + 10;
int dp[MAXN];
class Solution {
bool ok(char c){
return c >= '1' && c <= '9';
}
bool ok(char a, char b){
if(a == '1')
return b >= '0' && b <= '9';
if(a == '2')
return b >= '0' && b <= '6';
return false;
}
public:
int numDecodings(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(s.size() <= 0)
return 0;
dp[0] = 1;
for(int i = 1; i <= s.size(); ++ i){
dp[i] = 0;
if(ok(s[i - 1]))
dp[i] += dp[i - 1];
if(i >= 2)
if(ok(s[i - 2], s[i - 1]))
dp[i] += dp[i - 2];
}
return dp[s.size()];
}
};