当前位置:网站首页>967- letter combination of telephone number
967- letter combination of telephone number
2022-07-06 21:01:00 【-Lin Zeyu】
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 .

Their thinking
First, use a hash table to store all possible letters corresponding to each number , And then do the backtracking operation .
Maintaining a string during backtracking , Represents an existing alphabetic arrangement ( If you don't traverse all the numbers of the phone number , The existing alphabetic arrangement is incomplete ). The string is initially empty . Take one digit of the phone number at a time , Get all possible letters corresponding to the number from the hash table , And insert one of the letters after the existing alphabet , Then continue to process the last digit of the phone number , Until all the numbers in the phone number are processed , That is to get a complete alphabetic arrangement . And then go back , Traverse the rest of the alphabet .
Backtracking algorithm is used to find all feasible solutions , If a solution is found to be infeasible , It will abandon the infeasible solution . In this question , Because every letter corresponding to every number can enter the alphabet , So there is no infeasible solution , Just enumerate all the solutions directly .
Solution code
class Solution
{
public:
vector<string> letterCombinations(string digits)
{
vector<string> combinations;
if (digits.empty())
{
return combinations;
}
unordered_map<char, string> phoneMap
{
{
'2', "abc"},
{
'3', "def"},
{
'4', "ghi"},
{
'5', "jkl"},
{
'6', "mno"},
{
'7', "pqrs"},
{
'8', "tuv"},
{
'9', "wxyz"}
};
string combination;
backtrack(combinations, phoneMap, digits, 0, combination);
return combinations;
}
void backtrack(vector<string>& combinations,
const unordered_map<char, string>& phoneMap,
const string& digits,
int index,
string& combination)
{
if (index == digits.length())
{
combinations.push_back(combination);
}
else
{
char digit = digits[index];
const string& letters = phoneMap.at(digit);
for (const char& letter : letters)
{
combination.push_back(letter);
backtrack(combinations, phoneMap, digits, index + 1, combination);
combination.pop_back();
}
}
}
};
边栏推荐
- 审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
- 2022 portal crane driver registration examination and portal crane driver examination materials
- Pat 1085 perfect sequence (25 points) perfect sequence
- Manifest of SAP ui5 framework json
- [200 opencv routines] 220 Mosaic the image
- APS taps home appliance industry into new growth points
- Yyds dry goods count re comb this of arrow function
- @PathVariable
- Detailed explanation of knowledge map construction process steps
- 3D人脸重建:从基础知识到识别/重建方法!
猜你喜欢

Value of APS application in food industry

Manifest of SAP ui5 framework json

New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue

PHP online examination system version 4.0 source code computer + mobile terminal

Swagger UI教程 API 文档神器

拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条

全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
![[DIY]如何制作一款個性的收音機](/img/fc/a371322258131d1dc617ce18490baf.jpg)
[DIY]如何制作一款個性的收音機

Core principles of video games

Data Lake (VIII): Iceberg data storage format
随机推荐
2017 8th Blue Bridge Cup group a provincial tournament
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
C language games - minesweeping
How to turn a multi digit number into a digital list
基于STM32单片机设计的红外测温仪(带人脸检测)
7. Data permission annotation
[200 opencv routines] 220 Mosaic the image
OLED屏幕的使用
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Opencv learning example code 3.2.3 image binarization
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
每个程序员必须掌握的常用英语词汇(建议收藏)
【mysql】触发器
Logic is a good thing
[weekly pit] information encryption + [answer] positive integer factorization prime factor
R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
2022 nurse (primary) examination questions and new nurse (primary) examination questions
use. Net drives the OLED display of Jetson nano
What key progress has been made in deep learning in 2021?
Value of APS application in food industry