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
相关推荐
Hgfdsaqwr4 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
TracyCoder1234 小时前
LeetCode Hot100(15/100)——54. 螺旋矩阵
算法·leetcode·矩阵
开发者小天4 小时前
python中For Loop的用法
java·服务器·python
flushmeteor4 小时前
JDK源码-基础类-String
java·开发语言
毕设源码-钟学长4 小时前
【开题答辩全过程】以 基于ssm的空中停车场管理系统为例,包含答辩的问题和答案
java
老百姓懂点AI4 小时前
[RAG实战] 向量数据库选型与优化:智能体来了(西南总部)AI agent指挥官的长短期记忆架构设计
python
不愿是过客5 小时前
java实战干货——长方法深递归
java
u0109272715 小时前
C++中的策略模式变体
开发语言·c++·算法
2501_941837265 小时前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
小北方城市网6 小时前
Redis 分布式锁高可用实现:从原理到生产级落地
java·前端·javascript·spring boot·redis·分布式·wpf