LeetCode_Decode Ways

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:
bool check(char a, char b){
if(a == '')
return true;
if(a == '' && b>= '' && b<= '')
return true;
return false;
}
int numDecodings(string s) {
int len = s.size();
if(len == || s[] == '') return ;
vector<int> res(len+,);
res[] = ;
res[] = ;
for(int i = ; i < len ; ++i){
if(s[i] == '' && (s[i-] == ||s[i-]>''))
return ;
if(s[i]<=''&& s[i] > '')
res[i+] = res[i];
if(check(s[i-], s[i]))
res[i+] += res[i-];
} return res[len];
}
};
上一篇:华为2015 简单 字典输入法 java


下一篇:android小细节