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
相关推荐
yunhuibin25 分钟前
GoogLeNet学习
人工智能·python·深度学习·神经网络·学习
易辰君1 小时前
【Python爬虫实战】正则:中文匹配与贪婪非贪婪模式详解
开发语言·爬虫·python
秀儿还能再秀1 小时前
正则表达式核心语法 + Python的 re 库中常用方法
python·正则表达式
xcLeigh1 小时前
Python入门:Python3 正则表达式全面学习教程
python·学习·正则表达式·教程·python3
多恩Stone2 小时前
【C++ debug】在 VS Code 中无 Attach 调试 Python 调用的 C++ 扩展
开发语言·c++·python
XW01059993 小时前
4-11判断素数
前端·python·算法·素数
深蓝电商API3 小时前
爬虫增量更新:基于时间戳与哈希去重
爬虫·python
两万五千个小时3 小时前
构建mini Claude Code:06 - Agent 如何「战略性遗忘」(上下文压缩)
人工智能·python
两万五千个小时3 小时前
构建mini Claude Code:12 - 从「文件冲突」到「分身协作」:Worktree 如何让多 Agent 安全并行
人工智能·python·架构
yuki_uix3 小时前
为什么我的 Auth Token 藏在了 Network 面板的 Doc 里?
前端·python·debug