码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-06 12:27:03      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:following   from   +=   self   sel   str   nta   line   ted   



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个字母解码个数)

标签:following   from   +=   self   sel   str   nta   line   ted   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!