class Solution:
def numDecodings(self, s: str) -> int:
n=len(s)+1
dp=[1]*n
if s[0]=="0":dp[1]=0
for i in range(2,n):
if s[i-2:i]=="00" or (s[i-1]=="0" and int(s[i-2:i])>26):
dp[i]=0
elif s[i-1]=="0" :
dp[i]=dp[i-2]
elif int(s[i-2:i])>26 or s[i-2]=="0":
dp[i]=dp[i-1]
else :
dp[i]=dp[i-1]+dp[i-2]
return dp[n-1]
leetcode91. 解码方法
最新推荐文章于 2023-10-06 15:46:52 发布