当前位置:网站首页>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();
}
}
}
};
边栏推荐
- 全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- 什么是RDB和AOF
- What key progress has been made in deep learning in 2021?
- C language games - three chess
- Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
- 基于深度学习的参考帧生成
- Deployment of external server area and dual machine hot standby of firewall Foundation
- Common English vocabulary that every programmer must master (recommended Collection)
- 审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
猜你喜欢
PHP saves session data to MySQL database
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
Value of APS application in food industry
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
Design your security architecture OKR
使用.Net分析.Net达人挑战赛参与情况
【微信小程序】运行机制和更新机制
The mail command is used in combination with the pipeline command statement
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
随机推荐
Implementation of packaging video into MP4 format and storing it in TF Card
c#使用oracle存储过程获取结果集实例
【微信小程序】運行機制和更新機制
PG基础篇--逻辑结构管理(事务)
Pycharm remote execution
使用.Net驱动Jetson Nano的OLED显示屏
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
基于STM32单片机设计的红外测温仪(带人脸检测)
1_ Introduction to go language
(工作记录)2020年3月11日至2021年3月15日
SAP UI5 框架的 manifest.json
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
[200 opencv routines] 220 Mosaic the image
Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
User defined current limiting annotation
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
基于深度学习的参考帧生成
Mtcnn face detection
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!