当前位置:网站首页>【刷题记录】17. 电话号码的字母组合
【刷题记录】17. 电话号码的字母组合
2022-07-21 21:43:00 【InfoQ】
一、题目描述

输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
输入:digits = ""
输出:[]
输入:digits = "2"
输出:["a","b","c"]
- 0 <= digits.length <= 4
- digits[i] 是范围 ['2', '9'] 的一个数字。
二、思路分析
hashMap + DFS 深度优先搜索
- 从题目中可以看出,每一个数字都对应字符数组。我们用可以
map
来存这个对应的关系
- 其实每一个数字我们都可以看成一个树的节点,而对应的字符,就可以看作是这个节点下的分支
- 每多一个数字,就相当于在分支的节点下多一个相应的情况分支。
- 然后我们对这个树进行遍历即为我们想要的最终结果例如上面的示例 1:
代码实现
class Solution1 {
Map<Character, String> map = new HashMap<Character, String>() {{
put('2', "abc");
put('3', "def");
put('4', "ghi");
put('5', "jkl");
put('6', "mno");
put('7', "pqrs");
put('8', "tuv");
put('9', "wxyz");
}};
public List<String> letterCombinations(String ds) {
int length = ds.length();
List<String> res = new ArrayList<>();
if (length == 0) return res;
StringBuilder sb = new StringBuilder();
dfs(ds, 0, length, sb, res);
return res;
}
void dfs(String ds, int i, int n, StringBuilder sb, List<String> res) {
if (i == n) {
res.add(sb.toString());
return;
}
char key = ds.charAt(i);
String letters = map.get(key);
for (int j = 0; j < letters.length(); j++){
sb.append(letters.charAt(j));
dfs(ds, i +1 , n, sb, res);
sb.deleteCharAt(sb.length() -1);
}
}
}
复杂度分析
m
3
n
4
运行结果

总结
边栏推荐
- How to give selenium Chrome write extension intercepts or forwards requests
- Virtual machine dual network card configuration
- LSA类型,OSPF的优化以及拓扑配置
- Do you dare to use BigDecimal without mastering these pits?
- day2 poc与exp学习之pikachu字符注入
- The wave of the meta universe is shocking. Seize the opportunity and work together
- 长安全新最强SUV来袭,内饰很立体,科技感爆棚
- Morphology of machine learning foundation (5)
- How to choose the primary key UUID, self incrementing ID and snowflake algorithm of MySQL? (glory Collection Edition)
- How to choose sentinel vs. hystrix current limiting?
猜你喜欢
泰山OFFICE技术讲座:段落的边框,应该在哪一层处理
NFS shared storage service
Sentinel vs Hystrix 限流对比,该如何选择?
OAuth2.0 四种授权模式
[development tutorial 5] open source Bluetooth heart rate waterproof sports Bracelet - battery power detection
Audio and video (III) - Deep Foundation Excavation
如何抓住元宇宙中机遇
Opérations de déclaration des défauts
一文读懂Elephant Swap的LaaS方案的优势之处
How does MySQL data 900w+ achieve from 17s to 300ms? Charm of SQL optimization (glory Collection Edition)
随机推荐
Actual combat of flutter - customized keyboard (III)
How does redis like and cancel? (glory Collection Edition)
NFS shared storage service
6. User defined model construction and use of paddlepaddle
[游记]来自学长的馈赠2-2022.7.21
[译] Swift 编译器性能
【oops-framework】时间管理
QT: keyboard events, mouse events, timer small instances
HCIP第十二天
Chrome 开发者工具 network 显示 Provisional headers are shown 的几种原因
A frequency compensation method based on all phase FFT amplitude
Do you dare to use BigDecimal without mastering these pits?
从基础到进阶,100道测试开发面试题,进大厂涨薪必备
[mathematics of machine learning 01] countable sets and uncountable sets
如何抓住元宇宙中机遇
[travel notes] gifts from seniors 2-july 21, 2022
Research and application of the resource management framework of the world wide web of goods
Actual combat of flutter - customized keyboard (II)
google测试框架
Chang'an's new strongest SUV is coming. The interior is very three-dimensional, and the sense of science and technology is bursting