1 Star 0 Fork 0

Zhou Shihui/LeetCode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
17.电话号码的字母组合.java 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
Zhou Shihui 提交于 2022-07-18 09:33 +08:00 . 7.15
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
* @lc app=leetcode.cn id=17 lang=java
*
* [17] 电话号码的字母组合
*/
// @lc code=start
class LetterCombinations {
Map<String, String[]> map = new HashMap<String, String[]>() {
{
put("2", new String[] { "a", "b", "c" });
put("3", new String[] { "d", "e", "f" });
put("4", new String[] { "g", "h", "i" });
put("5", new String[] { "j", "k", "l" });
put("6", new String[] { "m", "n", "o" });
put("7", new String[] { "p", "q", "r", "s" });
put("8", new String[] { "t", "u", "v" });
put("9", new String[] { "w", "x", "y", "z" });
}
};
List<String> res = new ArrayList<>();
public List<String> letterCombinations(String digits) {
int n = digits.length();
if (n == 0) {
return res;
}
StringBuilder sb = new StringBuilder();
dfs(digits, 0, n, sb);
return res;
}
void dfs(String digits, int i, int n, StringBuilder sb) {
if (i == n) {
res.add(sb.toString());
return;
}
String key = digits.substring(i, i + 1);
String[] value = map.get(key);
for (String s : value) {
sb.append(s);
dfs(digits, i + 1, n, sb);
sb.deleteCharAt(sb.length() - 1);
}
}
}
// @lc code=end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/ImCaO/LeetCode.git
git@gitee.com:ImCaO/LeetCode.git
ImCaO
LeetCode
LeetCode
master

搜索帮助