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
相关推荐
cxyxiaokui0019 分钟前
线程池的“变形记”:核心线程数居然能随时变大变小?
java·面试
灵魂猎手16 分钟前
11. Mybatis SQL解析源码分析
java·后端·源码
艾醒39 分钟前
huggingface入门:如何使用国内镜像下载huggingface中的模型
算法
跟橙姐学代码43 分钟前
Python 集合:人生中最简单的真理,只有一次
前端·python·ipython
努力的小郑44 分钟前
别再说你会 new Object() 了!JVM 类加载的真相,绝对和你想的不一样
java·jvm·面试
艾醒1 小时前
huggingface入门:Tokenizer 核心参数与实战指南
算法
cxyxiaokui0011 小时前
论如何优雅地让AI“闭嘴”:深入SpringAI的流式停止与记忆难题
java·后端
偷心伊普西隆1 小时前
Python Excel 通用筛选函数
python·excel·pandas
嗝屁小孩纸1 小时前
使用EasyExcel自定义导出表格
java·excel
Warren981 小时前
Spring Boot 整合网易163邮箱发送邮件实现找回密码功能
数据库·vue.js·spring boot·redis·后端·python·spring