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
相关推荐
程序员杰哥18 分钟前
Fiddler抓包手机和部分app无法连接网络问题
自动化测试·软件测试·python·测试工具·智能手机·fiddler·测试用例
weixin_3077791328 分钟前
用Python和FastAPI构建一个完整的企业级AI Agent微服务脚手架
python·fastapi·web app
熊猫_豆豆31 分钟前
回调函数的作用与举例(Python版)
服务器·python·编程语法
AI Echoes42 分钟前
LangChain 使用语义路由选择不同的Prompt模板
人工智能·python·langchain·prompt·agent
JJJJ_iii1 小时前
【机器学习16】连续状态空间、深度Q网络DQN、经验回放、探索与利用
人工智能·笔记·python·机器学习·强化学习
CodeLongBear1 小时前
从Java后端到Python大模型:我的学习转型与规划
java·python·学习
ada7_1 小时前
LeetCode(python)——49.字母异位词分组
java·python·leetcode
我的xiaodoujiao1 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 23--数据驱动--参数化处理 Yaml 文件
python·学习·测试工具·pytest
晨尘光1 小时前
【pycharm 创建一个线程,在线程函数中增加的日志打印,日志打印了,但是打断点进不去】
ide·python·pycharm
databook2 小时前
manim边做边学--文字创建销毁的打字机效果
后端·python·动效