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

边栏推荐
猜你喜欢

Hwinfo hardware detection tool v7.26 green version

ApplicationContext 与 BeanFactory 区别(MS)

Huawei ENSP simulator configures ACL access control list

UTF encoding and character set in golang

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

Huawei ENSP simulator layer 3 switch

Three or two things about the actual combat of OMS system

Gobang go to work fishing tools can be LAN / man-machine

【C語言】符號的深度理解

迈动互联中标北京人寿保险
随机推荐
Golang面试整理 三 简历如何书写
2021 CCPC Harbin B. magical subsequence (thinking question)
华为模拟器ensp的路由配置以及连通测试
A quick start to fastdfs takes you three minutes to upload and download files to the ECS
LambdaQueryWrapper用法
[ 每周译Go ] 《How to Code in Go》系列文章上线了!!
c语言函数形参自增自减情况分析
Use of redis publish subscription
Word文档中标题前面的黑点如何去掉
【C語言】符號的深度理解
杰理之AD 系列 MIDI 功能说明【篇】
Jerry's ad series MIDI function description [chapter]
Day24:文件系统
华为ensp模拟器 给路由器配置DHCP
shp数据制作3DTiles白膜
Foxit pdf editor v10.1.8 green version
Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
Hwinfo hardware detection tool v7.26 green version
Jerry's ad series MIDI function description [chapter]
吐槽 B 站收费,是怪它没钱么?