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
相关推荐
努力学习的代码小白36 分钟前
源码学习04
python
Ray Wang1 小时前
Context上下文工程
python·学习·ai编程
梦影_1 小时前
Langchain简单快速上手教程(五)——聊天模型之流式传输
java·数据库·人工智能·python·langchain
ai2work1 小时前
17 · composer 进阶
python
L@ncor1 小时前
第二章 智能体发展史 · 学习笔记
人工智能·python
asdzx671 小时前
Python 实战:基于 Spire.PDF 为 PDF 文档添加自定义文本
python·pdf
m0_547486662 小时前
《Python数据分析与可视化项目教程》全套PPT课件2026
python·数据分析·数据可视化
hqyjzsb2 小时前
规划工商管理大学成长:搭建四层能力体系,重视高阶的 AI 能力建设
开发语言·人工智能·python·microsoft·职场和发展·数据挖掘·业界资讯
我不会起名字3222 小时前
一天一道算法题(35):电话号码的字母组合
java·数据结构·后端·python·leetcode·go·回溯
冯一川2 小时前
DeepSeek在Windows系统上部署
windows·python