# 给一个字符串,按如下规则把它翻译成字符串:1翻译成a,2翻译成b,...25翻译成z;一个数可以有多种翻译方式,比如122可以翻译成abb和kb还可以翻译成aw即3种翻译方式。计算一个数字有几种翻译方式
class Solution(object):
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
s = str(s)
dic = list()
for i in range(1,27):
dic.append(str(i))
dp = [0]*len(s)
if s[0] in dic:
dp[0] = 1
else:
return 0
for i in range(1,len(s)):
a = 0
b = 0
a = dp[i-1]
if s[i-1:i+1] in dic:
if i == 1:
b = 1
else:
b = dp[i-2]
# 加上注释这块代码就是表示:0对应a,不加就是1对应a
# if int(s[i-1])==0 or int(s[i])==0:
# b = 0
dp[i] = a+b
print(dp)
return dp[-1]