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