leetcode【每日一题】17. 电话号码的字母组合

题干

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

在这里插入图片描述

示例:

输入:“23”
输出:[“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

想法

类似于dfs
遍历到底了回退一层重新遍历
使用回溯

Java代码

class Solution {
     public List<String> letterCombinations(String digits) {
        List<String> combinations=new ArrayList<>();
      if (digits.length() == 0) {
            return combinations;
        }
        Map<Character,String> phoneMap=new HashMap<Character,String>(){{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(combinations, phoneMap, digits, 0, new StringBuffer());
        return combinations;

 
    }

    public void backtrack(List<String> combinations, Map<Character, String> phoneMap, String digits, int index, StringBuffer combination) {
        if(index==digits.length()){
            combinations.add(combination.toString());
        }
        else {
            char digit=digits.charAt(index);
            String letter=phoneMap.get(digit);
            int lettersCount=letter.length();
            for (int i=0;i<lettersCount;i++){
                combination.append(letter.charAt(i));
                backtrack(combinations,phoneMap,digits,index+1,combination);
                combination.deleteCharAt(index);
            }
        }

    }

}

另一种做法

嵌套循环
每个都遍历一遍

class Solution {
    public List<String> letterCombinations(String digits) {
        //数组num用来对应数字和案件
        String[] num={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        //用来存储最终的结果
        List<String> res=new ArrayList<>();
        if(digits.isEmpty())
            return res;
        //加上头节点
        res.add("");
        //遍历每个输入的字母
        for(char c:digits.toCharArray()){
            //c-'0' 高效获得c的位置
            res=combine(num[c-'0'],res);
        }
        return res;
    }
    //剩下的字母组合
        private List<String> combine(String str, List<String> list){
        List<String> res = new ArrayList<>();

        for(char c : str.toCharArray()){
            for(String s : list){
                res.add(s+c);
            }
        }
        
        return res;
    }
        
    }

我的leetcode代码都已经上传到我的githttps://github.com/ragezor/leetcode

猜你喜欢

转载自blog.csdn.net/qq_43491066/article/details/108233087