2020-11-03

21 篇文章 0 订阅

11/3
今天做了作业,比较烦
然后做的题目是17.电话号码的字母组合
这道题没有思路,用的是官方的题解,学习到了回溯的一部分内容,还有递归的使用。
具体代码如下:

class Solution {
    public List<String> letterCombinations(String digits) {
        var combinations = new ArrayList<String>();
        if (digits.length()==0){
            return combinations;
        }
        var phoneMap=new HashMap<Character,String>();
        phoneMap.put('2',"abc");
        phoneMap.put('3',"def");
        phoneMap.put('4',"ghi");
        phoneMap.put('5',"jkl");
        phoneMap.put('6',"mno");
        phoneMap.put('7',"pqrs");
        phoneMap.put('8',"tuv");
        phoneMap.put('9',"wxyz");
        backtrack(combinations,phoneMap,digits,0,new StringBuilder());
        return combinations;
    }
    private static void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuilder combination)
    {
        if (index==digits.length()){
            combinations.add(combination.toString());
        }else {
            char digit=digits.charAt(index);
            String letter = phoneMap.get(digit);
            int len=letter.length();
            for (int i = 0; i < len; i++) {
                combination.append(letter.charAt(i));
                backtrack(combinations,phoneMap,digits,index+1,combination);
                combination.deleteCharAt(index);
            }
        }
    }
    }
    ```
    这道题,难点在于构造出来的函数,首先,需要一个list集合来存储输出的结果,其次有一个hashmap集合来存放各个数字所对应的字母来选择输出,有个下标来指示目前指向到哪个输入的字符,这道题,需要消化理解才行,然后最后有一个递归。
    今天的任务就是思考完成这道题!
    啊啊啊啊,加油啊
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值