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]