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
相关推荐
m0_640309302 分钟前
Go语言怎么做链路追踪_Go语言分布式链路追踪教程【精选】.txt
jvm·数据库·python
m0_377618234 分钟前
CSS如何实现背景颜色的棋盘格分布_利用repeating-gradient
jvm·数据库·python
Sirius.z5 分钟前
第J1周:ResNet-50算法实战与解析
python
m0_746752306 分钟前
Less如何简化CSS复杂选择器_使用&连接符提升编写效率
jvm·数据库·python
2301_813599557 分钟前
HTML函数开发需要SSD吗_SSD对HTML函数开发效率影响【详解】
jvm·数据库·python
qq_342295828 分钟前
如何在 Pandas 中安全地对非空 DataFrame 执行行级操作
jvm·数据库·python
西西弗Sisyphus14 分钟前
PyTorch 中用于 主机(CPU)与设备(GPU)同步 的函数 torch.cuda.synchronize()
pytorch·python·synchronize
qq_3345635519 分钟前
MySQL如何实现数据库审计日志记录_开启通用日志与插件审计
jvm·数据库·python
无风听海20 分钟前
Python Union语法深度解析
python
阿里巴啦23 分钟前
一个 Python 视频处理工具链实战:下载、转录、摘要、字幕、诊断全打通 (已开源)
人工智能·python·whisper·视频下载·视频处理工具