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
相关推荐
无垠的广袤7 小时前
【工业树莓派 CM0 Dev Board】扩展板设计
linux·python·嵌入式硬件·pcb设计·模块化·传感器
Yolanda_20228 小时前
在vscode终端中可使用pip,但是cmd终端中找不到命令 pip问题的解决
vscode·python·conda·pip
2301_764441338 小时前
用动力学系统(微分方程)为 Kernberg 的客体关系单元提供数学化的操作定义,把“自体—客体“这对心理结构建模成一个二维耦合系统
数据结构·python·算法·数学建模
dogstarhuang8 小时前
用 Doubao-Seed-Evolving + Python 免费写一个网页正文提取工具(实战教程)
爬虫·python·ai编程
猫头虎9 小时前
什么是ZCode for GLM-5.2?
开发语言·人工智能·python·科技·算法·ai编程·ai写作
心运软件9 小时前
Python实战:中国大学排行榜数据采集与可视化大屏
后端·python
长不胖的路人甲9 小时前
什么是赫夫曼树(哈夫曼树 / Huffman Tree)
python·算法·霍夫曼树
魔镜er10 小时前
03-张量
人工智能·pytorch·python
37.2℃99510 小时前
Claude Design哪个公司技术好
python·设计模式
1570925113411 小时前
【无标题】
开发语言·python·算法