标签:style color io os ar for sp on amp
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: int numDecodings(std::string s) { if (s.empty()) return 0; int m=s.back()=='0'?0:1, n=1, res=m; for (int i=s.size()-2; i>=0; i--) { if (s[i]=='0') res=0; else res=m+(atoi(s.substr(i, 2).c_str())<=26?n:0); n=m; m=res; } return res; } };
标签:style color io os ar for sp on amp
原文地址:http://blog.csdn.net/akibatakuya/article/details/40477363