Leetcode 17. Letter Combinations of a Phone Number(python)

笨方法回溯啦

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        p_map=[" ","*","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
        solution=[]
        res=""
        if len(digits)==0: return []
        self.back_track(res,0,digits,p_map,solution)
        return solution

    def back_track(self,res,k,digits,p_map,solution):
        if k==len(digits):
            solution.append(res)
            return
        for i in range(len(p_map[int(digits[k])])):
            res+=p_map[int(digits[k])][i]
            #print res+"1",solution,k,len(digits)
            self.back_track(res,k+1,digits,p_map,solution)
            res=res[:-1]

  

posted @ 2016-04-06 10:58  colors  阅读(229)  评论(0编辑  收藏  举报