当前位置:网站首页>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();
}
}
}
};
边栏推荐
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- 正则表达式收集
- OSPF多区域配置
- use. Net analysis Net talent challenge participation
- 过程化sql在定义变量上与c语言中的变量定义有什么区别
- C language games - three chess
- 【DSP】【第一篇】开始DSP学习
- 【mysql】游标的基本使用
- 全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎
- Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
猜你喜欢
[asp.net core] set the format of Web API response data -- formatfilter feature
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
监控界的最强王者,没有之一!
Why do novices often fail to answer questions in the programming community, and even get ridiculed?
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
数据湖(八):Iceberg数据存储格式
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
Database - how to get familiar with hundreds of tables of the project -navicat these unique skills, have you got it? (exclusive experience)
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
随机推荐
[diy] how to make a personalized radio
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
快过年了,心也懒了
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
每个程序员必须掌握的常用英语词汇(建议收藏)
PHP online examination system version 4.0 source code computer + mobile terminal
(工作记录)2020年3月11日至2021年3月15日
Taylor series fast Fourier transform (FFT)
数据湖(八):Iceberg数据存储格式
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
PG basics -- Logical Structure Management (transaction)
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
面试官:Redis中有序集合的内部实现方式是什么?
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Design your security architecture OKR
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
Data Lake (VIII): Iceberg data storage format
I've seen many tutorials, but I still can't write a program well. How can I break it?