Leetcode17电话号码的组合

思路:用字典的形式保存号码的映射,实际组合是前一个数字串的组合加上后面一个数字的所有可能组合

python 复制代码
answer_dict={'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']}
class Solution:
    def letterCombinations(self, digits: str) -> list[str]:
        digits=digits.replace('1','')
        if not digits:
           return [] 
        digits_list=list(digits)
        answer_list=answer_dict[digits_list[0]][:]
        for digit in digits_list[1:]:
            current_answer=[]
            for each_answer in answer_list:
                for each_char in answer_dict[digit]:
                    current_answer.append(each_answer+each_char)
            answer_list=current_answer
        print(answer_list)
        return answer_list
相关推荐
外收内放9 小时前
Python与AI应用(项目开发实战:AI智能伴侣第一版)
python·学习·ai编程
绘梨衣5479 小时前
慢SQL排查手册
python·sql
troy1289 小时前
告别 Copilot?Codex、Claude Code、DeepSeek Harness、Kimi、ChatGPT 哪个更适合本地化部署,更有性价比?
人工智能·python·开源软件
IvanCodes9 小时前
Python 文件操作(十二):文件与目录的读写
开发语言·python
Y幽谷客18 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
伞伞悦读18 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人18 小时前
Windows删除图标缓存
windows·python·pyqt
qq_54702617919 小时前
Python 变量和简单数据类型
python
智搜广告20 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo
IpdataCloud20 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip