133.电话号码的字母组合

java 复制代码
class Solution {
    HashMap<Character,String> hm=new HashMap<>();
    List<String> res=new ArrayList<>();
    StringBuilder path=new StringBuilder();
    public List<String> letterCombinations(String digits) {
        if(digits==null||digits.length()==0){
            return res;
        }
        hm.put('1',"");
        hm.put('2',"abc");
        hm.put('3',"def");
        hm.put('4',"ghi");
        hm.put('5',"jkl");
        hm.put('6',"mno");
        hm.put('7',"pqrs");
        hm.put('8',"tuv");
        hm.put('9',"wxyz");
        hm.put('0',"");
        slout(digits,digits.length(),0);
        return res;
    }
    void slout(String digits,int le,int startIndex){
        if(path.length()==le){
            res.add(path.toString());
            return;
        }
        String st=hm.get(digits.charAt(startIndex));
        for(int i=0;i<st.length();i++){
            path.append(st.charAt(i));
            slout(digits,le,startIndex+1);
            path.deleteCharAt(path.length()-1);
        }
    }
}
python 复制代码
class Solution(object):
    def letterCombinations(self, digits):
        hm={
            '1':"",
            '2':"abc",
            '3':"def",
            '4':"ghi",
            '5':"jkl",
            '6':"mno",
            '7':"pqrs",
            '8':"tuv",
            '9':"wxyz",
            '0':"",
        }
        res=[]
        path=[]
        def slout(digits,le,startIndex):
            if len(path)==le:
                res.append("".join(path))
                return
            st=hm[digits[startIndex]]
            for i in range(len(st)):
                path.append(st[i])
                slout(digits,le,startIndex+1)
                path.pop()
        if digits is None or len(digits)==0:
            return res
        slout(digits,len(digits),0)
        return res
相关推荐
小宁爱Python17 分钟前
从零搭建 RAG 智能问答系统3:聊天信息持久化和登录注册
python
天才少女爱迪生1 小时前
LLVM(Low Level Virtual Machine)介绍
python·数据挖掘
碳酸的唐5 小时前
A* 工程实践全指南:从启发式设计到可视化与性能优化
python·神经网络
.格子衫.5 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多5 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
Swift社区6 小时前
LeetCode 394. 字符串解码(Decode String)
算法·leetcode·职场和发展
tt5555555555557 小时前
LeetCode进阶算法题解详解
算法·leetcode·职场和发展
让我们一起加油好吗7 小时前
【基础算法】DFS中的剪枝与优化
算法·深度优先·剪枝
Jabes.yang7 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂