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
相关推荐
猿榜5 分钟前
魔改编译-永久解决selenium痕迹(二)
javascript·python
广东数字化转型6 分钟前
java jar 启动应用程序
开发语言·python
费弗里38 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(4)
python·dash
辣辣y1 小时前
python基础day08
开发语言·python
花海如潮淹1 小时前
硬件产品研发管理工具实战指南
前端·python
张彦峰ZYF1 小时前
快速掌握Python编程基础
python
Json____1 小时前
使用python的 FastApi框架开发图书管理系统-前后端分离项目分享
开发语言·python·fastapi·图书管理系统·图书·项目练习
安思派Anspire1 小时前
LangGraph + MCP + Ollama:构建强大代理 AI 的关键(二)
人工智能·后端·python
站大爷IP2 小时前
Python文件与目录比较全攻略:从基础操作到性能优化
python
ahead~3 小时前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm