首页 > 其他 > 详细

91. Decode Ways(动态规划 26个字母解码个数)

时间:2018-04-06 11:18:51      阅读:212      评论:0      收藏:0      [点我收藏+]


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 class Solution:
 2     def numDecodings(self, s):
 3         """
 4         :type s: str
 5         :rtype: int
 6         """
 7         if s==‘‘:
 8             return 0
 9         
10         dp = [0] *(len(s)+1)
11         dp[0] = 1
12         dp[1] = int(s[0]!=0)
13         
14         for i in range(2,len(s)+1):
15             if(s[i-1]!=0):
16                 dp[i]=dp[i-1]
17                 
18             if s[i-2:i]>09 and s[i-2:i]<27:
19                 dp[i] +=dp[i-2]
20             
21         return dp[len(s)]
22 ##  1 2 1
23 ##  1 0 1
24 
25 
26  #dp[i] = dp[i-1] if s[i] != "0"
27         #       +dp[i-2] if "09" < s[i-1:i+1] < "27"

 

91. Decode Ways(动态规划 26个字母解码个数)

原文:https://www.cnblogs.com/zle1992/p/8724819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!