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
相关推荐
Sisphusssss9 小时前
香橙派5plus GPIO
linux·python·ubuntu
颜酱9 小时前
08 | 把维度值同步到 Elasticsearch(生成阶段)
人工智能·python·langchain
久久学姐9 小时前
基础转码学 AI:Java+Python 双语言入门,3 个月可落地实战项目
java·python·ai·转码·实战项目
ZHOU_WUYI9 小时前
4. light wam 模型loss计算过程
开发语言·人工智能·python
nwsuaf_huasir10 小时前
【无标题】
python
Python私教11 小时前
Codex 写出的代码能跑却算错钱:我用 3 个测试拆穿一次 AI 编程幻觉
python·单元测试·ai编程
Python私教11 小时前
我只写了一个 add 工具,终于把 MCP 的 Host、Client、Server 跑明白了
python·ai编程·mcp
小大宇12 小时前
python milvus 案例
开发语言·python·milvus
BerryS3N13 小时前
Java 后端转型大模型:Demo 能跑不等于能上线
java·人工智能·python·java后端·spring ai·langchain4j·大模型转型
制造数据与AI践行者老蒋13 小时前
智联工坊实战:从“金鱼记忆”到“记住了”:给制造Agent装上记忆芯片的完整指南
人工智能·python·langchain·制造