当前位置:网站首页>【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

边栏推荐
- .NET C#基础(6):命名空间 - 有名字的作用域
- Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
- 教育专家王中泽老师多年经验分享:家庭教育不是附庸品
- [deploy private warehouse based on harbor] 4 push image to harbor
- 【概率论与数理统计】猴博士 笔记 p41-44 统计量相关小题、三大分布的判定、性质、总体服从正态分布的统计量小题
- News web page display
- saltstack部署zabbix状态文件编写
- The difference between arrow function and ordinary function
- WPF 数据绑定(四)
- How to make time planning
猜你喜欢

Whether the ZABBIX monitoring host is online

Do you know what the quotation for it talent assignment service is? It is recommended that programmers also understand
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
![[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen](/img/93/d1014b07c924195e062dc83d69b14a.png)
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen

. Net C Foundation (6): namespace - scope with name

Leetcode hot topic 100 topic 6-10 solution

latex 各种箭头/带文字标号的箭头/可变长箭头
![[advanced concurrency] - thread pool summary](/img/69/dc8146dafc30f8a8efa012b67aa05c.png)
[advanced concurrency] - thread pool summary

Leetcode hot topic 100 topic 21-25 solution
随机推荐
資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
Web API、DOM
模块化笔记
Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
Saltstack deployment ZABBIX state file writing
[matlab printed character recognition] OCR printed letter + number recognition [including source code 1861]
常用问题排查工具和分析神器,值得收藏
Flutter 内外边距
Duality-Gated Mutual Condition Network for RGBT Tracking
RGB-D Salient Object Detection withCross-Modality Modulation and Selection
Summary of string processing skills III
【Matlab印刷字符识别】OCR印刷字母+数字识别【含源码 1861期】
The difference between arrow function and ordinary function
matplotlib的cmap
Starting from scratch (V) realize bullet positioning and animation
12. integer to Roman numeral
Luogu p1091 chorus formation (longest ascending subsequence)
Difference between byte and bit
Flutter 约束容器组件
开源漫画服务器Mango