标签:
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.
Dynamic Programming String
class Solution { int m_cnt; public: void numDecode(int dep, string s) { if(dep == s.size()) { m_cnt ++; return; } numDecode(dep+1, s); if((dep+1) < s.size()) { if( (s[dep] == ‘1‘ || s[dep] == ‘2‘) && (s[dep+1] >= ‘0‘ && s[dep+1] <= ‘6‘) ) numDecode(dep+2, s); } } int numDecodings(string s) { m_cnt = 0; numDecode(0, s); return m_cnt; } };
思路二:动态规划
标签:
原文地址:http://www.cnblogs.com/diegodu/p/4378723.html