Leetcode 91. Decode Ways
题目
解法1:recursion + memorization
这种方法是leetcode官方提供的一种,说实话我觉着理解起来不是那么直观,所以这边不多做解释
python代码如下:
class Solution:
def numDecodings(self, s: str) -> int:
# recursion with memorization
def dfs(index):
if index == len(s):
return 1
if s[index] == '0':
return 0
if index in memo:
return memo[index]
if index < len(s)-1:
ans = dfs(index+1) + (dfs(index+2) if (int(s[index:index+2]) <= 26) else 0)