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
相关推荐
WoY20202 分钟前
conda修改镜像源遇到的问题:defaults(默认镜像源)清不干净导致创建环境失败
linux·python·conda
渡我白衣9 分钟前
计算机组成原理(11):加法器
python·机器学习·numpy·pandas·matplotlib·计组·数电
龙腾AI白云27 分钟前
深度学习—卷积神经网络(3)
人工智能·python
qq_124987075332 分钟前
基于spark的西南天气数据的分析与应用(源码+论文+部署+安装)
大数据·分布式·爬虫·python·spark·毕业设计·数据可视化
STLearner36 分钟前
2025时空数据研究工作总结
大数据·人工智能·python·深度学习·学习·机器学习·智慧城市
2401_8414956438 分钟前
自然语言处理实战——基于BP神经网络的命名实体识别
人工智能·python·神经网络·算法·机器学习·自然语言处理·命名实体识别
七夜zippoe38 分钟前
Python元类编程-动态创建类的艺术
python·元类·高级编程·prepare·mro
明如正午42 分钟前
Kvaser使用Python收发报文示例
python·kvaser
q_302381955642 分钟前
宇树机器人又刷第一!具身智能靠强化学习解锁直立行走与快速奔跑
人工智能·python·单片机·机器人·ai编程
vvoennvv1 小时前
【Python TensorFlow】 TCN-BiGRU时间序列卷积双向门控循环神经网络时序预测算法(附代码)
python·神经网络·机器学习·gru·tensorflow·tcn