# 例如 12258 一共有 5 种,分别是 abbeh,lbeh,aveh,abyh,lyh。
def numDecodings(s="12258"):
def dfs(s, dp):
if len(s) == 0:
print(dp)
return
if len(s) >= 2 and int(s[:2]) <= 26:
dpcopy = dp.copy()
dpcopy.append(chr(ord('a')+int(s[:2])-1))
dfs(s[2:], dpcopy)
dpcopy = dp.copy()
dpcopy.append(chr(ord('a')+int(s[:1])-1))
dfs(s[1:], dpcopy)
return dfs(s, [])
numDecodings()
output:
['l', 'y', 'h']
['l', 'b', 'e', 'h']
['a', 'v', 'e', 'h']
['a', 'b', 'y', 'h']
['a', 'b', 'b', 'e', 'h']
标签:46,len,12258,翻译成,def,字符串,dp
From: https://www.cnblogs.com/aolob/p/17595875.html