17. Letter Combinations of a Phone Number ##backtracking


class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        #先把字典写出来
        d={'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']}
        
        if len(digits)==0:
            return None
        ##backtracking
        combine=[]
        for ele in d[digits[0]]:
            combine.append(ele)
        digits=digits[1:]
        while len(digits)>0:
            m=[]
            for c in d[digits[0]]:
                for s in combine:
                    m.append(s+c)
            combine=m
            digits=digits[1:]
        return combine
        

He published 183 original articles · won praise 91 · views 9999

Guess you like

Origin blog.csdn.net/weixin_45405128/article/details/104639736