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
相关推荐
Q_Q5110082857 分钟前
python+django/flask的宠物用品系统vue
spring boot·python·django·flask·node.js·php
hmbbcsm25 分钟前
练习python题目小记(五)
开发语言·python
蓝桉~MLGT26 分钟前
Python学习历程——文件
python·学习·策略模式
循环过三天27 分钟前
7.5、Python-匿名函数lambda
笔记·python·学习
仟濹41 分钟前
【Java 基础】3 面向对象 - this
java·开发语言·python
Dxy12393102161 小时前
Python一个类的特殊方法有哪些
开发语言·python
梅花141 小时前
基于Django的博客系统
后端·python·django·毕业设计·博客·博客系统·毕设
烤汉堡2 小时前
Python入门到实战:网络请求与数据获取
python
rimoyee2 小时前
[python探本] 内存数据存储机制
python