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

66 阅读1分钟

回溯

回溯要靠多做,多悟,画图也没用。下面就一步步来构建回溯(dfs)的框架

首先做好准备工作:

用数组代替哈希表提高效率,准备好各种数组,健壮性判断。。。

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

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return res;
        }
        //.......  
     }
     //......
}        

接着开始构建dfs

先写好递归出口,我们使用index来表示遍历到哪个号码了。当遍历完最后一个号码,就返回

public void dfs(String digits, int index) {
    if (index == digits.length()) {
        res.add(combination.toString());
    } else {
        //.....
    }
}

接着处理数据:

首先我们要知道遍历到哪个号码:

char digit = digits.charAt(index);

map中取出这个号码对应的字符串,并转换为字符数组,方便遍历:

char[] letters = phoneMap[digit - '0'].toCharArray();

接着遍历每个字符,以23为例子,先是abc中的a被选上:

for (int i = 0; i < letters.length; i++) {
    combination.append(letters[i]);
    //.....
}

选上一个a就够了,换下一个号码的字符,比如def中的d,此时就是ad

//....
char digit = digits.charAt(index); // digit == '3'
char[] letters = phoneMap[digit - '0'].toCharArray(); // letters: def

for (int i = 0; i < letters.length; i++) {
    combination.append(letters[i]); // ad
    dfs(digits, index + 1);
//....

ad被加入到res,回溯到def,把d删掉,for循环准备换下一个字母,也就是e

combination.deleteCharAt(index);

完整代码

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

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) {
            return res;
        }
        dfs(digits, 0);
        return res;
    }

    public void dfs(String digits, int index) {
        if (index == digits.length()) {
            res.add(combination.toString());
        } else {
            char digit = digits.charAt(index);
            char[] letters = phoneMap[digit - '0'].toCharArray();
            for (int i = 0; i < letters.length; i++) {
                combination.append(letters[i]);
                dfs(digits, index + 1);
                combination.deleteCharAt(index);
            }
        }

    }
}