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
相关推荐
下午见。14 小时前
Python基础入门:用Anaconda搭建环境的启蒙之旅
python
我叫汪枫14 小时前
Python 办公自动化入门:玩转 Excel 与 Word
python·word·excel
E_ICEBLUE14 小时前
三步完成 Markdown 到 Word/PDF 的转换:Python 教程
python·pdf·word·markdown·格式转换
后台开发者Ethan16 小时前
LangGraph ReAct应用
python·langgraph
f***686016 小时前
问题:Flask应用中的用户会话(Session)管理失效
后端·python·flask
爱吃面条的猿16 小时前
Python修改pip install 指定安装包的路径和默认镜像源
linux·python·pip
饭饭大王66616 小时前
Python 模块的概念与导入:从基础语法到高级技巧
java·服务器·python
Sunhen_Qiletian16 小时前
python语言应用实战--------网络爬虫篇 第二篇(selenium库)
爬虫·python·selenium
鄃鳕17 小时前
装饰器【Python】
开发语言·python·数码相机