17. 电话号码的字母组合(letterCombinations)
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = ""
输出:[]
示例 3:
输入:digits = "2"
输出:["a","b","c"]
提示:
0 <= digits.length <= 4
digits[i] 是范围 ['2', '9'] 的一个数字。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/letter-combinations-of-a-phone-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解1:
执行用时:40 ms, 在所有 Python3 提交中击败了65.06%的用户
内存消耗:16.2 MB, 在所有 Python3 提交中击败了18.65%的用户
通过测试用例:25 / 25
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
source = {
2: ["a", "b", "c"],
3: ["d", "e", "f"],
4: ["g", "h", "i"],
5: ["j", "k", "l"],
6: ["m", "n", "o"],
7: ["p", "q", "r", "s"],
8: ["t", "u", "v"],
9: ["w", "x", "y", "z"]
}
digits_len = len(digits)
res = []
if digits_len == 0:
return res
digits_list: list[int] = []
for s in digits:
digits_list.append(int(s))
res = source[digits_list[0]]
if digits_len == 1:
return res
for i in range(1, digits_len):
val_list = source[digits_list[i]]
temp_res = []
for rstr in res:
for vstr in val_list:
temp_res.append(rstr+vstr)
res = temp_res
return res
本文来自博客园,作者:tros,转载请注明原文链接:https://www.cnblogs.com/tros/p/17587292.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现