Given a digit string, return all possible letter combinations that the number could represent.
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
根据输入的数字输出所有可能的字母组合(按顺序)
深搜水过
class Solution:
# @param {string} digits
# @return {string[]}
def letterCombinations(self,digits):
res = []
tel = []
l = len(digits)
if l == 0:
return []
dic = [' ','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
def dfs(index):
if index >= l:
res.append(''.join(tel))
return
for i in dic[int(digits[index])-0]:
print i
tel.append(i)
dfs(index+1)
tel.pop()
return
dfs(0)
return res