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
相关推荐
std787927 分钟前
用PYTHON实现俄罗斯方块游戏案例
python·游戏·pygame
许泽宇的技术分享1 小时前
当AI学会“说人话“:Azure语音合成技术的魔法世界
后端·python·flask
光泽雨2 小时前
python学习基础
开发语言·数据库·python
裤裤兔2 小时前
python爬取pdf文件并保存至本地
chrome·爬虫·python·pdf·网络爬虫
Solyn_HAN2 小时前
非编码 RNA(ceRNA/lncRNA/circRNA)分析完整流程:从数据下载到功能验证(含代码模板)
python·bash·生物信息学·r
CesareCheung2 小时前
JMeter 进行 WebSocket 接口压测
python·websocket·jmeter
beijingliushao2 小时前
95-Python爬虫-正则表达式
爬虫·python·正则表达式
百***06012 小时前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
吃个糖糖3 小时前
pytorch 卷积操作
人工智能·pytorch·python