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
相关推荐
开发者小天4 小时前
python中calss的用法
开发语言·python
weixin_462446234 小时前
使用 Python 批量在 HTML 文件中插入自定义 div 元素
python·beautifulsoup·自动化脚本·html 批量处理
SeatuneWrite4 小时前
**AI漫剧软件2025推荐,解锁沉浸式二次元内容创作新体验
大数据·人工智能·python
Pyeako4 小时前
opencv计算机视觉--Harris角点检测&SIFT特征提取&图片抠图
人工智能·python·opencv·计算机视觉·harris角点检测·sift特征提取·图片抠图
艾莉丝努力练剑4 小时前
【AI时代的赋能与重构】当AI成为创作环境的一部分:机遇、挑战与应对路径
linux·c++·人工智能·python·ai·脉脉·ama
果粒蹬i4 小时前
从割裂到融合:MATLAB与Python混合编程实战指南
开发语言·汇编·python·matlab
木头左4 小时前
指数期权备兑策略的历史表现与Backtrader回测验证
python
yufuu984 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
qunaa01014 小时前
YOLOv26家具物品检测实战:基于Python和OpenCV实现家具识别系统
python·opencv·yolo
夕阳之后的黑夜4 小时前
Python脚本:为PDF批量添加水印
开发语言·python·pdf