当前位置:网站首页>【LeetCode】-- 17. Letter combination of telephone number
【LeetCode】-- 17. Letter combination of telephone number
2022-06-11 07:07:00 【Lina Belle~】
1. subject
Given a number only 2-9 String , Returns all the letter combinations it can represent . The answers can be returned in any order . The mapping of numbers to letters is given as follows ( Same as phone key ), Be careful 1 Does not correspond to any letter :

2. Example
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"]
3. analysis
(1) You need an array to map numbers to strings , This array does not change , Don't put it in a class , Otherwise every object will contain this array , Write it as a global
(2) Due to combination , So we should take... In turn digit The numbers in it , Combine the corresponding letters , You can use recursion
Source code :
class Solution {
public:
vector<string> letterCombinations(string digits) {
}
};The required return value is vector<string> Of , The return value is ret, Biography is digits, In order to get digit The value of each number in the , Recursion can be used to realize layer by layer composition , Here recursion has layers of parameters , The input parameters given by the native code are not enough , Rewrite a recursive function , The input parameter has numbers digits, The layer number i, also ret.
digits What is the length of , Just recurse a few layers , Such as digits by 236, The length is 3, Just recursion 3 layer :
(1)i by 0 when , That is the first. 1 layer , Numbers 2 The corresponding letters abc, And then take the number 1 Letters a, Are combined
(2) The composite object is 3 The corresponding letter , At this time i need ++ become 1 To the 2 layer , The digital 3 The corresponding letter def, Cycle to the first 1 Letters d
(3) Recursive second 3 layer ,i++ Turned into 2, Numbers 6 The corresponding letter is mno, Cycle to the first 1 Letters m
Choose one letter for each layer to combine , Recursion to the last level , You get the letter combination , Cycle the last layer , And then continue the cycle 2 layer , Then go to combination No 3 layer , Until the first 2 The layer combination is completed , This is the end of the combination 1 Layer 1 Letters , Recycle the first layer , Until the combination is completed 1 All letters of the layer , All the results are combined :

(3) If recursion ends at the last level , Put the string combination result into the result array
4. Code implementation
// Global variables , Has been the same
string num_str[10] = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
class Solution {
public:
void _letterCombinations(const string& digits, size_t i, string str,vector<string>& ret)
{
// Recursion to the last level , You have successfully combined a string , Put it in the result array
if(i == digits.size())
{
ret.push_back(str);
return;
}
int num = digits[i] - '0';
const string& letters = num_str[num];// No need for deep copy , Just a light copy , So add &
//digits No i A string corresponding to a number , Iterate according to the number of characters in the string
for(auto ch:letters)
{
_letterCombinations(digits,i+1,str+ch,ret);//str+ch Is the string being combined
}
}
vector<string> letterCombinations(string digits)
{
vector<string> ret;// Combined string result array
// Sentenced to empty
if(digits.empty())
{
return ret;
}
int i = 0;
string s;// String for each combination
_letterCombinations(digits, i, s,ret);// Recursive Composition
return ret;
}
};Call procedure recursively : Only with ad Call procedure for prefix letter combination , Everything else is similar to

边栏推荐
- Post exam summary
- Shutter restraint container assembly
- Leetcode hot topic 100 topic 21-25 solution
- 1734. arrangement after decoding XOR
- Duality-Gated Mutual Condition Network for RGBT Tracking
- Practice: how to reasonably design functions to solve practical problems in software development (I)
- 教育专家王中泽老师:家庭教育重在自己成长
- 顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
- Dynamically change the direction of this
- 【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题
猜你喜欢

Analysis of key points and difficulties of ES6 promise source code

Starting from scratch (V) realize bullet positioning and animation

The difference between arrow function and ordinary function

WPF 数据绑定(四)

Leetcode-104. Maximum Depth of Binary Tree

VTK vtkplane and vtkcutter use

洛谷P1091合唱队形(最长上升子序列)

Esp32 learning notes (49) - esp-wifi-mesh interface use

Listen to the left width of the browser to calculate the distance

.NET C#基础(6):命名空间 - 有名字的作用域
随机推荐
Library management system 2- demand analysis
Duality-Gated Mutual Condition Network for RGBT Tracking
教育专家王中泽老师多年经验分享:家庭教育不是附庸品
Zabbix 监控主机是否在线
latex 各种箭头/带文字标号的箭头/可变长箭头
資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
The difference between TCP and UDP
Appclips & tips (continuous update)
saltstack部署zabbix状态文件编写
Common troubleshooting tools and analysis artifacts are worth collecting
12. integer to Roman numeral
Promise. All capture error
News web page display
Records how cookies are carried in cross domain requests
June training (day 11) - matrix
Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
模块化笔记
Menu double linkage effect in uniapp
Modular notes
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台