leetcode 016 Letter Combinations of a Phone Number(Python)

来源:互联网 发布:魔法王座神翼升阶数据 编辑:程序博客网 时间:2024/06/05 09:18

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  
0 0
原创粉丝点击