动态规划题,注意0导致的小陷阱。
代码:
int numDecodings(string s) {
if (s.empty() || s[] < '' || s[] > '') return ; int sum = s[s.length() - ] >= '' && s[s.length() - ] <= '' ? : ;
int nextnext = ;
int next = sum; for (int i = s.length() - ; i >= ; i--) {
if (s[i] >= '' && s[i] <= '') {
if (s[i] == '' || (s[i] == '' && s[i + ] >= '' && s[i + ] <= ''))
sum = next + nextnext;
else if (s[i] == '')
sum = ;
else
sum = next;
nextnext = next;
next = sum;
}
else
return ;
} return sum;
}