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
相关推荐
淮北4941 小时前
pip虚拟环境包的问题
开发语言·python·pip
m0_706653232 小时前
用Python批量处理Excel和CSV文件
jvm·数据库·python
Yvonne爱编码2 小时前
JAVA数据结构 DAY5-LinkedList
java·开发语言·python
witAI2 小时前
**AI漫剧制作工具2025推荐,零成本实现专业级动画创作*
人工智能·python
qq_423233903 小时前
Python深度学习入门:TensorFlow 2.0/Keras实战
jvm·数据库·python
林深现海3 小时前
【刘二大人】PyTorch深度学习实践笔记 —— 第四集:反向传播(凝练版)
pytorch·python·numpy
菩提树下的凡夫3 小时前
Python 环境管理工具
开发语言·python
索荣荣4 小时前
JavaToken实战指南:从原理到应用
开发语言·python
Albert Edison4 小时前
【Python】函数
java·linux·python·pip
2401_836563184 小时前
用Python读取和处理NASA公开API数据
jvm·数据库·python