017 Letter Combinations of a Phone Number

遍历一遍就好

class Solution:
    def __init__(self):
        self.mapping = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
        self.ans = []
    # @param {string} digits
    # @return {string[]}
    def letterCombinations(self, digits):
        if digits == "":
            return []
        digits = list(digits)
        self.help(digits,"",0)
        return self.ans

    def help(self, digits, cur, level): 
        if level == len(digits):
            self.ans.append(cur)
            return 
        d = digits[level]
        for c in list(self.mapping[d]):
            cur += c
            self.help(digits, cur, level + 1)
            cur = cur[:-1]

 

posted @ 2015-07-08 08:12  dapanshe  阅读(97)  评论(0编辑  收藏  举报