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
相关推荐
梅如你6 小时前
【网盘直享】最新DEM数据分享(全球/全国/分省12.5m/30m/90m/250m/1000m)
图像处理·人工智能·python·计算机视觉
superman超哥7 小时前
仓颉热点代码识别深度解析
开发语言·后端·python·c#·仓颉
摸鱼仙人~7 小时前
如何对量化前后的模型进行评估
python
overmind7 小时前
oeasy玩py111列表_排序_sort_比较大小
python
JH灰色7 小时前
【大模型】-modelscope魔搭
python
STLearner8 小时前
AAAI 2026 | 时空数据(Spatial-temporal)论文总结[上](时空预测,轨迹挖掘,自动驾驶等)
大数据·人工智能·python·深度学习·机器学习·数据挖掘·自动驾驶
知行合一。。。8 小时前
Python--02--流程控制语句
开发语言·python
码农小卡拉8 小时前
Java多线程:CompletableFuture使用详解(超详细)
java·开发语言·spring boot·python·spring·spring cloud
Robot侠8 小时前
从 Python 到 Ollama:将微调后的 Llama-3/Qwen 一键导出为 GGUF
开发语言·python·llama·qwen
l1t8 小时前
Python 字符串反转方法
linux·开发语言·python