class Solution(object):
dp=[]
def numDecodings(self, s):
"""
:type s: str
:rtype: int
"""
if s=="":return 0
Len=len(s)
self.dp=[-1]*(Len+1)
return self.dfs(s)
def dfs(self,s):
Len=len(s)
if self.dp[Len]!=-1:return self.dp[Len]
cur=0
if Len==0:return 1
if s[0]=='0':return 0
if Len==1:return 1
if int(s[:2])<=26:
cur+=self.dfs(s[1:])
cur+=self.dfs(s[2:])
else:
cur+=self.dfs(s[1:])
self.dp[Len]=cur
return cur
LeetCode-91-Decode Ways DP
最新推荐文章于 2023-12-25 13:06:49 发布