java 力扣 17. 电话号码的字母组合

题目
java 力扣 17. 电话号码的字母组合_第1张图片
解法
①数组, 字符串 + 循环, 递归

class Solution {
    String[] map = {
            "",
            "",
            "abc",
            "def",
            "ghi",
            "jkl",
            "mno",
            "pqrs",
            "tuv",
            "wxyz"
        };
    List<String> ret = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if(digits.length() == 0){
            return ret;
        }
        char[] track = new char[digits.length()];
        trackback(track, digits, 0);
        return ret;
    }

    public void trackback(char[] track, String digits, int num){
        if(num == digits.length()){
            ret.add(new String(track));
            return;
        }
        char[] ch = map[digits.charAt(num) - '0'].toCharArray();
        for(char c : ch){
            track[num] = c;
            trackback(track, digits, num + 1);
        }
    }
}

你可能感兴趣的:(力扣,字符串,leetcode,java,数据结构,算法)