leetcode-17 电话号码组合

leetcode-17 电话号码组合

题目描述:

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

笛卡尔积问题

from itertools import product
class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        dic = {"2":"abc",
               "3":"def",
               "4":"ghi",
               "5":"jkl",
               "6":"mno",
               "7":"pqrs",
               "8":"tuv",
               "9":"wxyz"}
        res = []
        if not digits:
            return []
        for v in product(*[dic[t] for t in list(digits)]):
            res.append("".join(v))
        return res

for , 参考:负雪明烛

from itertools import product
class Solution:
    def letterCombinations(self, digits: str) -> List[str]:
        dic = {"2":"abc",
               "3":"def",
               "4":"ghi",
               "5":"jkl",
               "6":"mno",
               "7":"pqrs",
               "8":"tuv",
               "9":"wxyz"}
        if not digits:
            return []
        res = [""]
        for v in digits:
            res = [w+s for s in dic[v] for w in res]
        return res
posted @ 2019-08-01 11:14  静_渊  阅读(137)  评论(0编辑  收藏  举报