【2022初春】【LeetCode】17. 电话号码的字母组合

很基础的排列回溯问题,一个是java语法的处理,另一个是回溯取消状态的处理

class Solution {
    public List<String> letterCombinations(String digits) {
        List<String> res = new LinkedList<>();
        if (digits.length() == 0)
            return res;
        String[] map = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        back(digits,map,0,res,new StringBuilder());
        return res;

    }
    void back(String digits,String[] map,int i, List<String> res, StringBuilder stringbuilder){
        if(i==digits.length()){
            res.add(stringbuilder.toString());
            return ;
        }
        int num = Character.getNumericValue(digits.charAt(i));
        for(char c : map[num].toCharArray()){
            stringbuilder.append(c);
            back(digits,map,i+1,res,stringbuilder);
            stringbuilder.deleteCharAt(stringbuilder.length()-1);//明白个大概
        }
    }
}
上一篇:new String()创建了几个对象


下一篇:14.String相关,转换,StringBuilder,StringJoiner,String.join()