class Solution(object):
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
n = len(s)
f = [0]*(n+1)
f[0] = 1
f[1] = 1 if int(s[0])> 0 else 0
for i in range(2,n+1):
if int(s[i-1])> 0:
f[i] = f[i-1]
x = int(s[i-2:i])
if s[i-2]!='0' and x >=10 and x<=26:
f[i]+=f[i-2]
return f[n]
class Solution:
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if not s or s[0]=='0':#处理'0’的情况
return 0
n = len(s)
f = [0]*n
f[0]=1
for i in range(1,n):
num1 = int(s[i])
num2 = int(s[i-1:i+1])
if num1 > 0:
f[i]+=f[i-1]
if num2>9 and num2<27:
f[i]+=f[i-2] if i-2>=0 else 1# 第一个数的时候
return f[n-1]