class Solution { public: int numDecodings(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s==""||s[0]=='0') return 0; int count = s.length(); int pre = 1; int cur = 1; int code; for(int i = 1;i<count;i++) { code = s[i] + (s[i-1]-'0')*10 - '0'; if(code>26) { if(s[i]=='0') { return 0; } else{ cur = cur; pre = cur; } } else if(code == 0) { return 0; } else { if(s[i]=='0') { cur = pre; pre = 0; } else { code = cur; cur = cur + pre; pre = code; } } } return cur; } };
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.