标签:solution res dict pre str object obj type ring
mycode 68.26%
class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ if digits == "": return [] dict = {‘2‘:[‘a‘,‘b‘,‘c‘], ‘3‘:[‘d‘,‘e‘,‘f‘], ‘4‘:[‘g‘,‘h‘,‘i‘], ‘5‘:[‘j‘,‘k‘,‘l‘], ‘6‘:[‘m‘,‘n‘,‘o‘], ‘7‘:[‘p‘,‘q‘,‘r‘,‘s‘], ‘8‘:[‘t‘,‘u‘,‘v‘], ‘9‘:[‘w‘,‘x‘,‘y‘,‘z‘] } def dfs(index,temp): if index==self.len: self.res.append(temp) return num = digits[index] s = dict[num] for i in s: dfs(index+1,temp+i) self.res = [] self.len = len(digits) dfs(0,"") return self.res
参考:
def letterCombinations(digits): def dfs(num, string): if num == length: res.append(string) return for letter in dict[digits[num]]: dfs(num+1, string+letter) dict = {‘2‘:[‘a‘,‘b‘,‘c‘], ‘3‘:[‘d‘,‘e‘,‘f‘], ‘4‘:[‘g‘,‘h‘,‘i‘], ‘5‘:[‘j‘,‘k‘,‘l‘], ‘6‘:[‘m‘,‘n‘,‘o‘], ‘7‘:[‘p‘,‘q‘,‘r‘,‘s‘], ‘8‘:[‘t‘,‘u‘,‘v‘], ‘9‘:[‘w‘,‘x‘,‘y‘,‘z‘] } res = [] length = len(digits) dfs(0, ‘‘) return res
leetcode-mid-Linked list-17. Letter Combinations of a Phone Number
标签:solution res dict pre str object obj type ring
原文地址:https://www.cnblogs.com/rosyYY/p/10969946.html