您的位置:首页 > 编程语言 > Python开发

leetcode 016 Letter Combinations of a Phone Number(Python)

2015-06-11 22:17 711 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python leetcode dfs