17、电话号码的字母组合【中等】
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = “23”
输出:[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
示例 2:
输入:digits = “”
输出:[]
示例 3:
输入:digits = “2”
输出:[“a”,“b”,“c”]
提示:
0 <= digits.length <= 4
digits[i] 是范围 [‘2’, ‘9’] 的一个数字。
方法一:回溯
1.1 思路分析
其实思路很好想,难点在于如何实现以及回溯法的使用,下面是官方题解,看不懂直接看代码就行。
首先使用哈希表存储每个数字对应的所有可能的字母,然后进行回溯操作。
回溯过程中维护一个字符串,表示已有的字母排列(如果未遍历完电话号码的所有数字,则已有的字母排列是不完整的)。该字符串初始为空。每次取电话号码的一位数字,从哈希表中获得该数字对应的所有可能的字母,并将其中的一个字母插入到已有的字母排列后面,然后继续处理电话号码的后一位数字,直到处理完电话号码中的所有数字,即得到一个完整的字母排列。然后进行回退操作,遍历其余的字母排列。
回溯算法用于寻找所有的可行解,如果发现一个解不可行,则会舍弃不可行的解。在这道题中,由于每个数字对应的每个字母都可能进入字母组合,因此不存在不可行的解,直接穷举所有的解即可。
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/solution/dian-hua-hao-ma-de-zi-mu-zu-he-by-leetcode-solutio/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1.2 代码实现
class Solution {
public List<String> letterCombinations(String digits) {
List<String> combinations = new ArrayList<String>();
int n = digits.length();
if (n == 0) return combinations;
Map<Character, String> digMap = new HashMap<Character, String>();
digMap.put('2', "abc");
digMap.put('3', "def");
digMap.put('4', "ghi");
digMap.put('5', "jkl");
digMap.put('6', "mno");
digMap.put('7', "pqrs");
digMap.put('8', "tuv");
digMap.put('9', "wxyz");
backtrack(combinations, digMap, 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 letters = phoneMap.get(digit);
int lettersCount = letters.length();
for (int i=0; i<lettersCount; ++i){
combination.append(letters.charAt(i));
backtrack(combinations, phoneMap, digits, index+1, combination);
combination.deleteCharAt(index);
}
}
}
}