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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值