当前位置:网站首页>【leetcode】17. Letter combination of telephone number
【leetcode】17. Letter combination of telephone number
2022-06-29 00:26:00 【Chinese fir sauce_】
subject :
17. Letter combination of telephone number
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 .
to flash back :
The backtracking algorithm has three main points :
- choice
Determines which branches you have at each node , Help you build a spatial tree of solutions .
The choice of this question is , Multiple letters corresponding to each number , Choose to translate into one of the letters , Just continue recursion - constraint condition
For pruning , Cut out the subtree that does not meet the constraints , Avoid invalid searches . This question doesn't seem to reflect - The goal is
Determines when to capture solutions , Or cut the subtree that cannot be solved , Backtracking in advance . When the pointer of the scanned number reaches the end, the solution can be added to the solution set .
Ideas :
- Starting from the root node , After arriving at a node , A series of choices will be made to update the path according to the current status and topic meaning (path) The elements in .
- Status path (path) In this question, it means the element in a combination .
- In this question, we will select a letter from the currently available letter set and put it in path in .
- In addition, we use a hash table to store the mapping between numbers and letters
- When the leaf node is reached , Indicates that a combination has been found .
- After arriving at the leaf node, we will go back , That is, return to the previous node , Then make a new choice to calculate another combination .
- path It's like a stack structure , While returning to the previous node , You need to delete path The last element added to the .
- After traversing the leaf nodes of the entire tree , It means you have found all the combinations .
dfs + to flash back :
class Solution {
List<String> result = new ArrayList<>();
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 digits) {
if (digits.length() == 0) {
return result;
}
backtracking(digits, 0, new StringBuilder());
return result;
}
public void backtracking(String digits, int start, StringBuilder path) {
if (path.length() == digits.length()) {
result.add(path.toString());
return;
}
String letters = map.get(digits.charAt(start));
for (int j = 0; j < letters.length(); j++) {
path.append(letters.charAt(j));
backtracking(digits, start + 1, path);
path.deleteCharAt(path.length() - 1);
}
}
}
边栏推荐
- 运营级智慧校园系统源码 智慧校园小程序源码+电子班牌+人脸识别系统
- Easy to use free ppt template
- [communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)
- 每日一练:删除有序数组中的重复项
- Give you a project, how will you carry out performance testing (I)
- Bug risk level
- Daily practice: delete duplicates in the ordered array
- Encapsulation of JDBC connection and disconnection database
- Is it safe to open an account on the flush
- Zoom with mouse wheel
猜你喜欢

Haskell configuring vs code development environment (june2022)

Reasons for high price of optical fiber slip ring

Trois questions PWN

12.物体检测Mask-Rcnn
![[leetcode] 522. Longest special sequence II violence + double pointer](/img/88/3ddeefaab7e29b8eeb412bb5c3e9b8.png)
[leetcode] 522. Longest special sequence II violence + double pointer

Reprint: VTK notes - clipping and segmentation - irregular closed loop clipping -vtkselectpolydata class (black mountain old demon)

Comics | goodbye, postman! One stop collaboration makes apipost more fragrant!

机器视觉系统的配件及工作过程

Leetcode daily question: implementing strstr()
![[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)](/img/5d/bee9a6e50384a7e05e9a71e9fbed9a.jpg)
[communication] wide band source DOA estimation method based on incoherent signal subspace (ISM)
随机推荐
12. object detection mask RCNN
Typescript-- section 4: Functions
Daily question 1: missing numbers
var、let、const 三者的区别
11.目标分割
TypeScript -- 第二节:变量声明
运营级智慧校园系统源码 智慧校园小程序源码+电子班牌+人脸识别系统
好用免费的PPT模板
TypeScript -- 第一节:基础类型
TypeScript --第三节:接口
The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been
Notes: three ways to define setters and Getters
6.28 learning content
10. Yolo series
随笔记:定义setter和getter的三种方式
Précautions d'installation et d'utilisation des joints rotatifs
Reasons for high price of optical fiber slip ring
Phoenix installation tutorial
每日一题:数组中数字出现的次数2
矩 阵 压 缩