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
相关推荐
山楂树の2 小时前
Python 模块、包、面向对象编程
python
青少儿编程课堂3 小时前
蓝桥杯真题解析:三维偏序的CDQ分治(全国青少年信息素养大赛与电子学会编程考级)
c++·python·算法·bfs·信息学竞赛
geovindu3 小时前
python:Lasso
开发语言·后端·python·算法
3095566664 小时前
股票数据落库前的去重与主键设计:一次 SQLite 写入实录
java·开发语言·jvm·数据库·redis·python·sqlite
CyrusYiVPP4 小时前
决策树背答案背到 24 片叶子?剪枝三刀砍回 5 片,测试集从 0.749 涨回 0.845,ML数学基础第13期
人工智能·python
用户298698530144 小时前
Python 批量处理 Word 水印:文字与图片水印的添加及移除
python·api
承渊政道4 小时前
【Python编程快速上手—让繁琐工作自动化】(字典和结构化数据:用键值对搭出棋盘和游戏背包)
python·游戏·pycharm·自动化·字典和结构化数据
Wellijohn5 小时前
让 AI 能改数据库,但不能背着你改:我给 MCP Server 装了一道审批闸
python·架构·mcp
AI创界者5 小时前
【无标题】Python 进阶实战(五十九章):分布式限流、熔断降级与背压控制实战
人工智能·python·aigc
山楂树の5 小时前
Python 函数:参数、作用域、Lambda 与类型注解
开发语言·python