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
相关推荐
Ares-Wang17 分钟前
AI》》欧氏距离、曼哈顿距离 切比雪夫距离 等
人工智能·python
陈eaten25 分钟前
windows上协调多版本python以及虚拟环境
开发语言·windows·python·pycharm·pip·虚拟环境·py
一晌小贪欢29 分钟前
告别 `datetime` 混乱:使用 Python 类型注解构建健壮的时间处理管道
开发语言·python·时间·时间类型·时间模块
嘛?25070130 分钟前
Python高阶基础
python
li星野34 分钟前
哈希表通关八题:从两数之和到LRU缓存,手撕高频面试题(Python + C++)
python·缓存·散列表
yaoxin52112335 分钟前
401. Java 文件操作基础 - 使用 Buffered Stream I/O 写入文本文件
java·开发语言·python
E_ICEBLUE1 小时前
如何提取 Word 文档中的表格并导出为 Excel(Python 教程)
python·word·excel
极光代码工作室1 小时前
基于NLP的智能问答系统设计
python·深度学习·自然语言处理·nlp
lbb 小魔仙1 小时前
Python 多模态 AI 应用开发实战:用 GPT-4o + LangChain 构建智能视觉助手
人工智能·python·langchain
江南十四行1 小时前
Python元类编程——从type到metaclass的深度探索
开发语言·python