LeetCode 17. 电话号码的字母组合
程序员文章站
2022-04-16 23:53:32
...
题目描述
给定一个数字字符串,返回数字所有可能表示的字母组合。
下面给出数字到字母的映射(和电话号码一样)。
输入:数字字符串 "23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路
用Map<Character, String[]>
存放每个数字对应的字母数组
考虑深度优先搜索,dfs中输入一个step
变量表示目前进行到第几位数字
递归结束后一定记得str = str.substring(0, str.length() - 1);
将字符串最后一位删除
代码
class Solution {
List<String> res = new ArrayList<String>();
Map<Character, String[]> map = new HashMap<Character, String[]>();
String str = "";
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) return res;
map.put('2', new String[]{"a", "b", "c"});
map.put('3', new String[]{"d", "e", "f"});
map.put('4', new String[]{"g", "h", "i"});
map.put('5', new String[]{"j", "k", "l"});
map.put('6', new String[]{"m", "n", "o"});
map.put('7', new String[]{"p", "q", "r", "s"});
map.put('8', new String[]{"t", "u", "v"});
map.put('9', new String[]{"w", "x", "y", "z"});
dfs(digits,0);
return res;
}
public void dfs(String digits, int step) {
int numLen = digits.length();
if (step == digits.length()) {
res.add(str);
return;
}
char c = digits.charAt(step);
String[] strArray = map.get(c);
for (String s : strArray) {
str += s;
dfs(digits, step + 1);
str = str.substring(0, str.length() - 1);
}
}
}