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.

思路:

思路1,利用NP的想法递归,但是时间复杂度太大。
思路2,为了简化时间复杂度,我们采用动态规划的方法。例如,当我们知道了n-2长度的字符串能够解释的数目以及n-1长度的字符串能够解释的数目时,我们可以判读如下两个条件:
1)若第n个字符在1到9之间,则n长度的字符串能够解释的数目包含n-1长度字符串能够解释的数目。
2)若第n-1个字符与第n个字符可以解释为一个字母时,则n长度的字符串能够解释的数目包含n-2长度字符串能够解释的数目。
 

代码:

思路1:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. class Solution {  
  2. public:  
  3. int count;  
  4.     int numDecodings(string s) {  
  5.         if(s.size()==0)  
  6.         {  
  7.             return 0;  
  8.         }  
  9.         count=0;  
  10.         Decode(s,0,0);  
  11.         return count;  
  12.     }  
  13.       
  14.     void Decode(string s, int begin, int end)  
  15.     {  
  16.         if(begin>=s.size())  
  17.         {  
  18.             count++;  
  19.         }  
  20.         else if(end>=s.size())  
  21.         {  
  22.             return;  
  23.         }  
  24.         else  
  25.         {  
  26.             int num=0;  
  27.             for(int i =begin;i<=end;i++)  
  28.             {  
  29.                 num*=10;  
  30.                 num+=s[i]-'0';  
  31.             }  
  32.             if(num>=1&&num<=26)  
  33.             {  
  34.                 Decode(s,end+1,end+1);  
  35.             }  
  36.             if(num>=1&&num<=2)  
  37.             {  
  38.                 Decode(s,begin,end+1);  
  39.             }  
  40.         }  
  41.     }  
  42. };  

思路2
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. class Solution {  
  2. public:  
  3.     int numDecodings(string s) {  
  4.         if(s.size() == 0){  
  5.             return 0;  
  6.         }  
  7.         else if(s.size() == 1){  
  8.             return s[0] != '0' ? 1 : 0;  
  9.         }  
  10.         else if(s.size() == 2){  
  11.             return (s[0] != '0' && s[1] != '0'? 1 : 0) + ((s[0] != '0' && (char2int(s[0]) * 10 + char2int(s[1])) <= 26) ? 1 : 0);  
  12.         }  
  13.           
  14.         int* dp = new int[s.size()];  
  15.         dp[0] = s[0] != '0' ? 1 : 0;  
  16.         dp[1] = (s[0] != '0' && s[1] != '0'? 1 : 0) + ((s[0] != '0' && (char2int(s[0]) * 10 + char2int(s[1])) <= 26) ? 1 : 0);  
  17.           
  18.         for(int i = 2; i < s.size(); i++){  
  19.             dp[i] = 0;  
  20.             if(s[i] != '0'){  
  21.                 dp[i] += dp[i-1];  
  22.             }  
  23.               
  24.             if(s[i-1] != '0' && (char2int(s[i-1]) * 10 + char2int(s[i])) <= 26){  
  25.                 dp[i] += dp[i-2];  
  26.             }  
  27.         }  
  28.           
  29.         return dp[s.size() - 1];  
  30.     }  
  31.       
  32.     int char2int(char c){  
  33.         return c - '0';  
  34.     }  
  35. };  

转自:http://blog.csdn.net/lanxu_yy/article/details/17306167

猜你喜欢

转载自chriszeng87.iteye.com/blog/2215218