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
相关推荐
努力写A题的小菜鸡1 分钟前
PyTorch 两种卷积写法彻底对比:F.conv2d 函数式 vs nn.Conv2d 类实战(超详细入门笔记)
python
golfscript2 分钟前
Playwright Python:微软出的浏览器自动化库
python·其他·microsoft·自动化
云烟成雨TD3 分钟前
Agent Scope Java 2.x 系列【5】智能体抽象层
java·人工智能·agent
happygrilclh6 分钟前
赚外快了:等离子表面处理机电源算法需求说明
算法
charliedev7 分钟前
Jedi:Python 自动补全与静态分析的实用工具
开发语言·python·其他
阿伟AI说7 分钟前
Codex 桌面版接入国产模型系列二:Codex++
java·开源软件·ai编程·腾讯云ai代码助手
ji1985944321 分钟前
MATLAB 求散点曲线斜率
开发语言·算法·matlab
kaikaile199525 分钟前
MATLAB 实现:Koch & Zhao 图像水印算法(DCT域)
开发语言·算法·matlab
装不满的克莱因瓶25 分钟前
自然语言处理中的分词——从语言切分到模型输入的第一步
人工智能·pytorch·python·深度学习·ai·自然语言处理
love_muming28 分钟前
链表每日一练
java·开发语言·数据结构·链表·idea·每日一练