LeetCode 17. Letter Combinations of a Phone Number
程序员文章站
2022-05-04 08:41:42
先特判,再创建一个String数组来存储数字和字母之间的对应关系,剩下的就是经典的DFS了。class Solution { public List letterCombinations(String digits) { List res = new ArrayList<>(); if(digits == null || digits.length() == 0){ re...
先特判,再创建一个String数组来存储数字和字母之间的对应关系,剩下的就是经典的DFS了。
class Solution {
public List<String> letterCombinations(String digits) {
List<String> res = new ArrayList<>();
if(digits == null || digits.length() == 0){
return res;
}
String[] map = new String[] {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
backtrack(digits, 0, new StringBuilder(), map, res);
return res;
}
private void backtrack(String digits, int index, StringBuilder sb, String[] map, List<String> res){
if(index == digits.length()){
res.add(sb.toString());
return;
}
for(char c: map[digits.charAt(index) - '2'].toCharArray()){
sb.append(c);
backtrack(digits, index + 1, sb, map, res);
sb.deleteCharAt(sb.length() - 1);
}
}
}
本文地址:https://blog.csdn.net/qq_31361913/article/details/107680974
上一篇: Java 基础学习 day_04
下一篇: 一致性Hash分析
推荐阅读
-
Leetcode 17. Letter Combinations of a Phone Number(python)
-
LeetCode 17. Letter Combinations of a Phone Number
-
Letter Combinations of a Phone Number(C++)
-
leetcode17:Letter Combinations of a Phone Number
-
LeetCode 17. 电话号码的字母组合 Letter Combinations of a Phone Number
-
leetcode--17. Letter Combinations of a Phone Number
-
LeetCode 17. Letter Combinations of a Phone Number
-
17. Letter Combinations of a Phone Number (Python) dfs(递归 recursion) + 迭代(iterative)
-
Letter Combinations of a Phone Number(C++)