电话号码的字母组合--力扣
Posted 穿迷彩服的鲨鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电话号码的字母组合--力扣相关的知识,希望对你有一定的参考价值。
前言
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
一、示例
示例 1:
输入:digits = “23”
输出:[“ad”,“ae”,“af”,“bd”,“be”,“bf”,“cd”,“ce”,“cf”]
示例 2:
输入:digits = “”
输出:[]
示例 3:
输入:digits = “2”
输出:[“a”,“b”,“c”]
二、代码解析
1.回溯+字符串数组
代码如下(示例):
//字符串数组+回溯
//深度优先搜索
string tempStr;
vector<string> result;
vector<string> strArr = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
void dfs(int index, string digits)
{
if (index == digits.size())
{
result.emplace_back(tempStr);
}
else
{
int num = digits[index] - '0';//ASCII转化
for (int i = 0; i < strArr[num].size(); i++)
{
tempStr.push_back(strArr[num][i]);
dfs(index + 1, digits);
tempStr.pop_back();
}
}
}
vector<string> letterCombinations(string digits)
{
if (digits.size() == 0)
{
return result;
}
dfs(0, digits);
return result;
}
结果
2.回溯+哈希表
代码如下(示例):
void backtrack(vector<string>& combinations, const unordered_map<char, string>& photoMap, const string& digits, int index, string& combination)
{
if (index==digits.size())
{
combinations.emplace_back(combination);
}
else
{
char digit = digits[index];
const string& letters = photoMap.at(digit);
for (auto& letter : letters)
{
combination.push_back(letter);
backtrack(combinations, photoMap, digits, index + 1, combination);
combination.pop_back();
}
}
}
//回溯+哈希表
//首先使用哈希表存储每个数字对应的所有可能的字母,然后进行回溯操作。
vector<string> letterCombinations(string digits)
{
vector<string> combinations;
if (digits.empty())
{
return combinations;
}
unordered_map<char, string> photoMap
{
{'2',"abc"},
{'3',"def"},
{'4',"ghi"},
{'5',"jkl"},
{'6',"mno"},
{'7',"pqrs"},
{'8',"tuv"},
{'9',"wxyz"},
};
string combination;
backtrack(combinations, photoMap, digits, 0, combination);
return combinations;
}
结果
测试
代码如下(示例):
#include<vector>
#include <iostream>
#include<unordered_map>
using namespace std;
void backtrack(vector<string>& combinations, const unordered_map<char, string>& photoMap, const string& digits, int index, string& combination)
{
if (index == digits.size())
{
combinations.emplace_back(combination);
}
else
{
char digit = digits[index];
const string& letters = photoMap.at(digit);
for (auto& letter : letters)
{
combination.push_back(letter);
backtrack(combinations, photoMap, digits, index + 1, combination);
combination.pop_back();
}
}
}
//回溯+哈希表
//首先使用哈希表存储每个数字对应的所有可能的字母,然后进行回溯操作。
vector<string> letterCombinations(string digits)
{
vector<string> combinations;
if (digits.empty())
{
return combinations;
}
unordered_map<char, string> photoMap
{
{'2',"abc"},
{'3',"def"},
{'4',"ghi"},
{'5',"jkl"},
{'6',"mno"},
{'7',"pqrs"},
{'8',"tuv"},
{'9',"wxyz"},
};
string combination;
backtrack(combinations, photoMap, digits, 0, combination);
return combinations;
}
//字符串数组+回溯
//深度优先搜索
/*
string tempStr;
vector<string> result;
vector<string> strArr = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
void dfs(int index, string digits)
{
if (index == digits.size())
{
result.emplace_back(tempStr);
}
else
{
int num = digits[index] - '0';//ASCII转化
for (int i = 0; i < strArr[num].size(); i++)
{
tempStr.push_back(strArr[num][i]);
dfs(index + 1, digits);
tempStr.pop_back();
}
}
}
vector<string> letterCombinations(string digits)
{
if (digits.size() == 0)
{
return result;
}
dfs(0, digits);
return result;
}*/
int main()
{
vector<string> str = letterCombinations("23");
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
return 0;
}
结果
总结
以上是关于电话号码的字母组合--力扣的主要内容,如果未能解决你的问题,请参考以下文章