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
相关推荐
2501_907136822 小时前
python 界面元素控件库工具,可以看到python的可视控件和使用方法
python·软件需求
2301_765703142 小时前
开发一个简单的Python计算器
jvm·数据库·python
yj15582 小时前
客厅阳台改卧室需要注意什么?
python
boss-dog2 小时前
关于强化学习入门理解和示例
python·强化学习
一只理智恩2 小时前
筹备计划·江湖邀请令!!!
python·langchain
Sagittarius_A*2 小时前
角点检测:Harris 与 Shi-Tomasi原理拆解【计算机视觉】
图像处理·人工智能·python·opencv·计算机视觉
进击的小头2 小时前
陷波器实现(针对性滤除特定频率噪声)
c语言·python·算法
LitchiCheng2 小时前
Mujoco 开源机械臂 RL 强化学习避障、绕障
人工智能·python·开源
A先生的AI之旅2 小时前
2026-1-30 LingBot-VA解读
人工智能·pytorch·python·深度学习·神经网络
丝瓜蛋汤2 小时前
微调生成特定写作风格助手
人工智能·python