dfs,递归拼接每个字符得到最终的字符串
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param num string字符串
# @return string字符串一维数组
#
class Solution:
def phoneNumber(self , num: str) -> List[str]:
# write code here
chars = {"2":"abc", "3":"def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9": "wxyz"}
res= []
def dfs(s, t):
nonlocal res
if len(t) == len(num):
res.append(t)
return
for i in range(s, len(num)):
for d in chars[num[i]]:
dfs(i + 1, t + d)
dfs(0, "")
return res