前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >17. 电话号码的字母组合

17. 电话号码的字母组合

作者头像
SingYi
发布2023-10-19 18:46:10
1450
发布2023-10-19 18:46:10
举报
文章被收录于专栏:Lan小站Lan小站

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

1697127863090.webp
1697127863090.webp
代码语言:javascript
复制
# @Time    : 2023/10/13 00:00
# @Author  : Lan
# @File    : 17. 电话号码的字母组合.py
# @Software: PyCharm
# @link    : https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/
"""
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
"""
from typing import List


class Solution:
    key_value = [
        "",
        "",
        "abc",
        "def",
        "ghi",
        "jkl",
        "nmo",
        "pqrs",
        "tuv",
        "wxyz"
    ]
    result = []
    temp = []

    def backtracking(self, digits, index):
        if index == len(digits):
            if self.temp:
                self.result.append(''.join(self.temp))
            return
        letters = self.key_value[int(digits[index])]
        for i in range(len(letters)):
            self.temp.append(letters[i])
            self.backtracking(digits, index + 1)
            self.temp.pop()

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


print(Solution().letterCombinations(''))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023年10月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档