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
相关推荐
上班职业摸鱼人16 小时前
python文件中导入另外一个模块这个模块
python
永远是夏天16 小时前
Python面向对象编程(OOP)全教程:从入门到实战(附案例)
python
动感小麦兜16 小时前
服务器搭建
linux·服务器·python
Pocker_Spades_A17 小时前
在家写的代码,办公室接着改?Jupyter通过cpolar实现远程访问这么玩
ide·python·jupyter
m5655bj18 小时前
使用 Python 高效复制 Excel 行、列、单元格
开发语言·python·excel
龙言龙论18 小时前
身份证信息批量处理系统:从入门到实战(附exe工具+核心源码)
数据库·python
m0_6265352018 小时前
代码分析 长音频分割为短音频
javascript·python·音视频
Wpa.wk18 小时前
自动化测试环境配置-java+python
java·开发语言·python·测试工具·自动化
带刺的坐椅18 小时前
AI 应用工作流:LangGraph 和 Solon AI Flow,我该选谁?
java·python·ai·solon·flow·langgraph
工业互联网专业19 小时前
图片推荐系统_django+spider
python·django·毕业设计·源码·课程设计·spider·图片推荐系统