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 分钟前
jupyter Kernel Disconnected崩溃的修复
ide·python·jupyter
Freak嵌入式14 分钟前
MicroPython LVGL基础知识和概念:显示与多屏管理
开发语言·python·github·php·gui·lvgl·micropython
枕布响丸辣18 分钟前
Python 操作 MySQL 数据库从入门到精通
数据库·python·mysql
The_Ticker44 分钟前
印度股票实时行情API(低成本方案)
python·websocket·算法·金融·区块链
ZC跨境爬虫1 小时前
Scrapy工作空间搭建与目录结构解析:从初始化到基础配置全流程
前端·爬虫·python·scrapy·自动化
EAIReport1 小时前
国外网站数据批量采集技术实现路径
开发语言·python
Ulyanov1 小时前
基于ttk的现代化Python音视频播放器:UI设计与可视化技术深度解析
python·ui·音视频
Freak嵌入式1 小时前
MicroPython LVGL基础知识和概念:时序与动态效果
开发语言·python·github·php·gui·lvgl·micropython
zhangzeyuaaa2 小时前
Python 中的 Map 和 Reduce 详解
开发语言·python
七夜zippoe2 小时前
Java技术未来展望:GraalVM、Quarkus、Helidon等新趋势探讨
java·开发语言·python·quarkus·graaivm·helidon