当前位置:网站首页>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();
}
}
}
};
边栏推荐
- [DIY]如何制作一款個性的收音機
- No Yum source to install SPuG monitoring
- Xcode6 error: "no matching provisioning profiles found for application"
- C language games - three chess
- OLED屏幕的使用
- Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
- Swagger UI tutorial API document artifact
- Spiral square PTA
- Redis insert data garbled solution
- C language operators
猜你喜欢
监控界的最强王者,没有之一!
Opencv learning example code 3.2.3 image binarization
(work record) March 11, 2020 to March 15, 2021
面试官:Redis中有序集合的内部实现方式是什么?
数据湖(八):Iceberg数据存储格式
Manifest of SAP ui5 framework json
APS taps home appliance industry into new growth points
【OpenCV 例程200篇】220.对图像进行马赛克处理
[DSP] [Part 1] start DSP learning
3D人脸重建:从基础知识到识别/重建方法!
随机推荐
Summary of different configurations of PHP Xdebug 3 and xdebug2
OSPF多区域配置
Pytest (3) - Test naming rules
C language games - three chess
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
"Penalty kick" games
What key progress has been made in deep learning in 2021?
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
[diy] self designed Microsoft makecode arcade, official open source software and hardware
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
性能测试过程和计划
【微信小程序】运行机制和更新机制
2017 8th Blue Bridge Cup group a provincial tournament
数据湖(八):Iceberg数据存储格式
基于STM32单片机设计的红外测温仪(带人脸检测)
Manifest of SAP ui5 framework json
面试官:Redis中有序集合的内部实现方式是什么?
快过年了,心也懒了
2022 portal crane driver registration examination and portal crane driver examination materials
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix