LeetCode(力扣)17. 电话号码的字母组合Python

LeetCode17. 电话号码的字母组合

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/letter-combinations-of-a-phone-number/
LeetCode(力扣)17. 电话号码的字母组合Python_第1张图片

代码

class Solution:
    def __init__(self):
        self.letterMap = [
            "",     # 0
            "",     # 1
            "abc",  # 2
            "def",  # 3
            "ghi",  # 4
            "jkl",  # 5
            "mno",  # 6
            "pqrs", # 7
            "tuv",  # 8
            "wxyz"  # 9
        ]
        self.result = []
        self.s = ""

    def backtracking(self, digits, index):
        if index == len(digits):
            self.result.append(self.s)
            return
        digit = int(digits[index])
        letter = self.letterMap[digit]
        for i in range(len(letter)):
            self.s += letter[i]
            self.backtracking(digits, index + 1)
            self.s = self.s[:-1]

    def letterCombinations(self, digits: str) -> List[str]:
        if len(digits) == 0:
            return []
        self.backtracking(digits, 0)
        return self.result

你可能感兴趣的:(python,leetcode,开发语言,算法,职场和发展)