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
相关推荐
岁岁的O泡奶几秒前
NSSCTF_reverse_[SWPUCTF 2022 新生赛]base64——[HDCTF 2023]easy_re
经验分享·python·逆向
阿Y加油吧1 分钟前
二分查找进阶:旋转排序数组的两道经典题深度解析
数据结构·算法
范什么特西4 分钟前
MyEclipse8.5配置
java·ide·myeclipse
wgzrmlrm745 分钟前
Django怎么优雅发送邮件_Python配置SMTP后端实现异步通知
jvm·数据库·python
想带你从多云到转晴5 分钟前
05、数据结构与算法---栈与队列
java·数据结构·算法
無限進步D5 分钟前
蓝桥杯赛后总结
算法·蓝桥杯·竞赛
QuZero10 分钟前
ReentrantLock principle
java·算法
kcuwu.11 分钟前
Python 数据分析实战:NumPy、Pandas、Matplotlib 融合
python·数据分析·numpy
m0_7167652311 分钟前
数据结构--顺序表的插入、删除、查找详解
c语言·开发语言·数据结构·c++·学习·算法·visual studio
Thomas214311 分钟前
skill分享 iterm2 jupyter jumpserver
ide·python·jupyter