当前位置:网站首页>[leetcode] 17. Letter combination of telephone number
[leetcode] 17. Letter combination of telephone number
2022-07-04 21:33:00 【Xiaoqu classmate】
17. Letter combination of telephone number
Digression :
Continue today LeetCode, I found that I was selected accidentally ,《 Data structure and algorithm field content list 》, It all depends on the support of all the big guys , Thank you thank you .
Next , Continue to brush the questions , analysis , I hope it can be used by students who want to learn algorithms , Help .
subject :
Given a number only 2-9 String , Returns all the letter combinations it can represent . You can press In any order return .
The mapping of numbers to letters is given as follows ( Same as phone key ). Be careful 1 Does not correspond to any letter .
Example 1:
Input :digits = "23"
Output :["ad","ae","af","bd","be","bf","cd","ce","cf"]
Example 2:
Input :digits = ""
Output :[]
Example 3:
Input :digits = "2"
Output :["a","b","c"]
Tips :
0 <= digits.length <= 4
digits[i] Is the scope of [‘2’, ‘9’] A number of .
Their thinking :
The difficulty of this question is simple , First , See all the combinations , Your first reaction is probably to go back , Yes , you 're right , This problem can be solved by backtracking .
You can use one first map, To store all the letters corresponding to the numbers ..
Use backtracking , Exhaustively , List all possible solutions .
Reference code :
class Solution {
// Set the global list to store the final results
List<String> list = new ArrayList<>();
public List<String> letterCombinations(String digits) {
if (digits == null || digits.length() == 0) {
return list;
}
// The initial number corresponds to all the numbers , For direct correspondence 2-9, Added two invalid strings ""
String[] numString = {
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
// Iterative processing
backTracking(digits, numString, 0);
return list;
}
// Get one string per iteration , So we will design a lot of string splicing , So choose the more efficient one here StringBuild
StringBuilder temp = new StringBuilder();
// such as digits If "23",num by 0, be str Express 2 Corresponding abc
public void backTracking(String digits, String[] numString, int num) {
// Traversing all the strings at one time, recording the string obtained at one time
if (num == digits.length()) {
list.add(temp.toString());
return;
}
//str At present num The corresponding string
String str = numString[digits.charAt(num) - '0'];
for (int i = 0; i < str.length(); i++) {
temp.append(str.charAt(i));
//c
backTracking(digits, numString, num + 1);
// Remove the last one and try again
temp.deleteCharAt(temp.length() - 1);
}
}
}
边栏推荐
- 网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
- Huawei ENSP simulator configures DHCP for router
- Procurement in software development
- Lambdaquerywrapper usage
- JS卡牌样式倒计时天数
- 华为模拟器ensp的路由配置以及连通测试
- Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
- Huawei ENSP simulator configures ACL access control list
- admas零件名重复
- B站视频 声音很小——解决办法
猜你喜欢
maya灯建模
B站视频 声音很小——解决办法
[wechat applet] collaborative work and release
Jerry added the process of turning off the touch module before turning it off [chapter]
IIC (STM32)
How was MP3 born?
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
Huawei ENSP simulator realizes communication security (switch)
随机推荐
Methods of improving machine vision system
Gobang go to work fishing tools can be LAN / man-machine
IIC (STM32)
Jerry's ad series MIDI function description [chapter]
admas零件名重复
Huawei simulator ENSP common commands
IIC (STM32)
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
MP3是如何诞生的?
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
[wechat applet] collaborative work and release
Compréhension approfondie du symbole [langue C]
华为ensp模拟器 配置ACL访问控制列表
NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??
redis发布订阅的使用
Roast B station charges, is it because it has no money?
Flutter WebView示例
【活动早知道】LiveVideoStack近期活动一览
Redis pipeline
heatmap.js图片热点热力图插件