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
相关推荐
程序员爱钓鱼2 分钟前
Python 编程实战 · 进阶与职业发展:自动化运维(Ansible、Fabric)
后端·python·trae
rising start7 分钟前
二、python面向对象高级
开发语言·python
虎头金猫7 分钟前
随时随地处理图片文档!Reubah 加cpolar的实用体验
linux·运维·人工智能·python·docker·开源·visual studio
Yue丶越8 分钟前
【Python】基础语法入门(二)
android·开发语言·python
郝学胜-神的一滴1 小时前
Effective Python 第52条:用subprocess模块优雅管理子进程
linux·服务器·开发语言·python
万粉变现经纪人1 小时前
如何解决 pip install 编译报错 ‘cl.exe’ not found(缺少 VS C++ 工具集)问题
开发语言·c++·人工智能·python·pycharm·bug·pip
r***11331 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
___波子 Pro Max.2 小时前
Python类型注解详解与应用
python
1***Q7842 小时前
Python增强现实案例
开发语言·python·ar
Q26433650233 小时前
【有源码】spark与hadoop-情感挖掘+画像建模的携程酒店评价数据分析可视化系统-基于机器学习的携程酒店评价情感分析与竞争态势可视化
大数据·hadoop·python·机器学习·数据分析·spark·毕业设计