标签:
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.
思路:
这是一个动态规划的问题。考察输入串中第i,i-1,i-2的三个字符。设decode_ways[i]为到第i个字符为止,找到的解码个数,s[i]为输入串中第i个字符。那么s[i-1]和s[i]可以有几种可能:
由此写出状态转移方程和代码。
状态转移方程:
1.如果S[i] == ‘0‘,如果S[i-1]存在且为‘1‘或者‘2‘,F[i] = F[i-1],否则无解;
2.如果S[i] != ‘0‘
如果S[i-1]==‘1‘,F[i] = F[i-1] + F[i-2](例如"xxxxx221",可以是"xxxxx22"+"1",也可以是"xxxx2"+"21");
如果S[i-1]==‘2‘,当S[i] <= ‘6‘时,F[i] = F[i-1] + F[i-2] (最大的Z为"26","27""28""29"不存在),当S[i] > ‘6‘时,F[i] = F[i-1] (例如"xxxxxx28",只能是"xxxxxx2" + "8")。
代码:
class Solution { public: int numDecodings(string s) { int n = s.length(); if (n<1) { return 0; } vector<int> dp(n+1, 0); dp[0] = 1; for (int i=1; i<n+1; i++) { if (i==1) { if (s[i-1]==‘0‘) { return 0; } else { dp[i] = 1; } } else { if (s[i-1]==‘0‘) { if (s[i-2]==‘1‘ || s[i-2]==‘2‘) { dp[i] = dp[i-2]; } else { return 0; } } else { if (s[i-2]==‘1‘ || (s[i-2]==‘2‘ && s[i-1]<=‘6‘)) { dp[i] = dp[i-1]+dp[i-2]; } else { dp[i] = dp[i-1]; } } } } return dp[n]; } };
参考:
1. http://blog.csdn.net/magisu/article/details/16942907
2. http://www.tuicool.com/articles/JVrMBr
标签:
原文地址:http://www.cnblogs.com/jellyang/p/4401776.html