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
相关推荐
喵手13 小时前
Python爬虫零基础入门【第八章:项目实战演练·第3节】上线与运维入门:定时运行、日志轮转、失败告警(轻量版)!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·定时运行·日志轮转
weixin_6600967813 小时前
flash-attention总是安装失败
python·flash-attention
yaoxin52112313 小时前
303. Java Stream API - 查找元素
java·windows·python
子午13 小时前
【2026计算机毕设】蔬菜识别系统~Python+深度学习+人工智能+算法模型+TensorFlow
人工智能·python·深度学习
kong790692813 小时前
Python 调用大模型(LLM)
人工智能·python·大模型llm
深蓝电商API13 小时前
Selenium 爬取 Canvas 渲染的数据图表
爬虫·python·selenium
Just right13 小时前
python安装包问题
开发语言·python
hhy_smile13 小时前
Function in Python
python
dxz_tust13 小时前
flow match简单直观理解
开发语言·python·深度学习·扩散模型·流匹配·flow match
写代码的【黑咖啡】13 小时前
Python 中的时间序列特征自动提取工具:tsfresh
开发语言·python