代码拉取完成,页面将自动刷新
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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。