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
相关推荐
MediaTea6 分钟前
Python OOP 设计思想 13:封装服务于演化
linux·服务器·前端·数据库·python
love530love7 分钟前
突破 ComfyUI 环境枷锁:RTX 3090 强行开启 comfy-kitchen 官方全后端加速库实战
人工智能·windows·python·cuda·comfyui·triton·comfy-kitchen
wang6021252188 分钟前
流式输出注意点
python·状态模式·fastapi
未定义.22110 分钟前
第3篇:UI自动化核心操作:输入、点击、弹窗、下拉框全场景实战
运维·python·ui·自动化·jenkins·集成测试·pytest
276695829213 分钟前
vercel 安全检测逆向 x-vercel-challenge-solution
开发语言·python·solution·vercel-solution·x-vercel·vercel逆向·ensun
dagouaofei15 分钟前
AI PPT 工具怎么选?5个维度对比6款产品
人工智能·python·powerpoint
深蓝电商API22 分钟前
Scrapy日志系统详解与生产环境配置
爬虫·python·scrapy
Irene.ll23 分钟前
DAY25 异常处理
python
努力学习的小洋27 分钟前
Python训练打卡Day4:缺失值处理
开发语言·python
郝学胜-神的一滴27 分钟前
Python类属性与实例属性详解及MRO算法演进
开发语言·python·程序人生·算法