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
相关推荐
神仙别闹18 小时前
基于 C++和 Python 实现计算机视觉
c++·python·计算机视觉
hongjianMa18 小时前
【论文阅读】Hypercomplex Prompt-aware Multimodal Recommendation
论文阅读·python·深度学习·机器学习·prompt·推荐系统
饼干,19 小时前
第23天python内容
开发语言·python
酷柚易汛智推官20 小时前
基于librespot的定制化Spotify客户端开发:开源替代方案的技术实践与优化
python·开源·酷柚易汛
雪碧聊技术20 小时前
requests入门
python·requests·请求头的user-agent
面向星辰20 小时前
机器学习过拟合和正则化
python
浔川python社20 小时前
《Python 小程序编写系列》(第三部):简易文件批量重命名工具
python·小程序·apache
QD.Joker21 小时前
高德MCP服务接入
python
孤客网络科技工作室21 小时前
孤客截图工具 Pro - 从开发到打包的完整指南
python
Q_Q196328847521 小时前
python+django/flask的医院财务管理系统
spring boot·python·django·flask·node.js