当前位置:网站首页>[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);
}
}
}

边栏推荐
- Jerry's ad series MIDI function description [chapter]
- Jerry's ad series MIDI function description [chapter]
- Redis transaction
- Render function and virtual DOM
- How was MP3 born?
- Three or two things about the actual combat of OMS system
- Jerry added the process of turning off the touch module before turning it off [chapter]
- [C language] deep understanding of symbols
- 旋变串判断
- 【公开课预告】:视频质量评价基础与实践
猜你喜欢

FastDfs的快速入门,三分钟带你上传下载文件到云服务器

每日一题-LeetCode556-下一个更大元素III-字符串-双指针-next_permutation

Configuration of DNS server of Huawei ENSP simulator

At the right time, the Guangzhou station of the city chain science and Technology Strategy Summit was successfully held

OMS系统实战的三两事

Redis:Redis配置文件相关配置、Redis的持久化

Daily question -leetcode1200- minimum absolute difference - array - sort

WGCNA分析基本教程总结

Day24: file system

CAD中能显示打印不显示
随机推荐
OMS系统实战的三两事
Redis transaction
Word文档中标题前面的黑点如何去掉
Roast B station charges, is it because it has no money?
B站视频 声音很小——解决办法
Redis:Redis配置文件相关配置、Redis的持久化
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
PS vertical English and digital text how to change direction (vertical display)
Configuration of DNS server of Huawei ENSP simulator
async await 在map中使用
redis RDB AOF
奋斗正当时,城链科技战略峰会广州站圆满召开
Why does invariant mode improve performance
华为ensp模拟器 实现多个路由器的设备可以相互访问
宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
torch. Tensor and torch The difference between tensor
Test case (TC)
WGCNA analysis basic tutorial summary
仿ps样式js网页涂鸦板插件