当前位置:网站首页>[record of question brushing] 17 Letter combination of telephone number
[record of question brushing] 17 Letter combination of telephone number
2022-07-22 12:27:00 【InfoQ】
One 、 Title Description

Input :digits = "23"
Output :["ad","ae","af","bd","be","bf","cd","ce","cf"]
Input :digits = ""
Output :[]
Input :digits = "2"
Output :["a","b","c"]
- 0 <= digits.length <= 4
- digits[i] Is the scope of ['2', '9'] A number of .
Two 、 Thought analysis
hashMap + DFS Depth-first search
- As can be seen from the title , Each number corresponds to a character array . We can use it
map
To save the corresponding relationship
- In fact, every number can be regarded as a node of a tree , And the corresponding character , It can be regarded as a branch under this node
- Every extra number , It is equivalent to one more corresponding branch under the node of the branch .
- Then we traverse the tree to get the final result we want, such as the example above 1:
Code implementation
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);
}
}
}
Complexity analysis
m
3
n
4
Running results

summary
边栏推荐
- 黑盒测试的概念及测试方法
- 墙纸教程–精美水下效果
- Effect of database index on write operation
- SQL bypass case
- How does redis like and cancel? (glory Collection Edition)
- Student management system based on jsp/servlet
- 《架构设计2.0大型分布式系统架构方法论与实践》三高笔记
- Swift compiler performance
- Legend setup tutorial GOM engine micro terminal setting method
- An open source template that can generate a free personal blog in only three steps: vdoing template
猜你喜欢
spark学习笔记(二)——IO(input&output)流(字节流、字符流、缓冲流)
【FPGA教程案例34】通信案例4——基于FPGA的QPSK调制信号产生,通过matlab测试其星座图
Chrome 开发者工具 network 显示 Provisional headers are shown 的几种原因
Effect of database index on write operation
OAuth2.0 四种授权模式
Quick understanding of PKI system
Flutter实战-自定义键盘(二)
Oauth2.0 four authorization modes
A 15-year-old ABAP veteran's suggestion: understanding these basic knowledge is beneficial to ABAP development
网络安全--ESP8266烧录、测试、擦除WiFi杀手(详细教程、附所有工具下载地址)
随机推荐
知识点复习(持续更新版)
How to use proxy IP address
Force deduction record: Sword finger offer (1) - jz3-12
《架构设计2.0大型分布式系统架构方法论与实践》三高笔记
Envoy分布式链路追踪
Taishan Office Technology Lecture: which layer should the paragraph border be handled
Ideas of literature reading
【oops-framework】全局事件
网络空间资产测绘
QQ新表情|实力演绎你的生活
Ten suggestions on how to do research efficiently for graduate students
history.go()和history.back(),history.go()不刷新
6. User defined model construction and use of paddlepaddle
中国经济网:“元宇宙”炙手可热
[quick start tutorial 5] crazy shell · open source formation UAV - flight control firmware burning
Swift compiler performance
用数据说话,凭用户甄选 | 云商店榜单每月见
As a test, how to effectively communicate with unreasonable developers
JMeter advanced performance test response results are saved locally
Legend setup tutorial GOM engine micro terminal setting method