# 电话号码的字母组合
给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = ""
输出:[]
示例 3:
输入:digits = "2"
输出:["a","b","c"]
提示:
0 <= digits.length <= 4
digits[i]
是范围 ['2', '9']
的一个数字。
以下错误的选项是?
## aop
### before
```cpp
#include
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
string digits = "23";
vector res;
res = sol.letterCombinations(digits);
for (auto i : res)
cout << i + ' ';
return 0;
}
```
## 答案
```cpp
string letterMap[10] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
class Solution
{
public:
void combinStr(const string &digits, size_t index, const string &str, vector &strs)
{
if (index == digits.size())
{
strs.push_back(str);
return;
}
string letters = letterMap[digits[index] - '0'];
for (size_t i = 0; i < letters.size(); i++)
{
combinStr(digits, index, str + letters[i], strs);
}
}
vector letterCombinations(string digits)
{
vector strs;
if (digits.empty())
return strs;
size_t index = 0;
string str;
combinStr(digits, index, str, strs);
return strs;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector result;
vector letterCombinations(string digits)
{
string temp;
if (digits.length() == 0)
return result;
getAns(digits, 0, temp, result);
return result;
}
void getAns(string digits, int start, string temp, vector &result)
{
if (temp.size() == digits.length())
result.push_back(temp);
else
{
vector let = getLet(digits[start]);
for (int i = 0; i < let.size(); i++)
{
temp.append(1, let[i]);
getAns(digits, start + 1, temp, result);
temp.pop_back();
}
}
}
vector getLet(char i)
{
vector let;
if (i == '2')
{
let.push_back('a');
let.push_back('b');
let.push_back('c');
}
else if (i == '3')
{
let.push_back('d');
let.push_back('e');
let.push_back('f');
}
else if (i == '4')
{
let.push_back('g');
let.push_back('h');
let.push_back('i');
}
else if (i == '5')
{
let.push_back('j');
let.push_back('k');
let.push_back('l');
}
else if (i == '6')
{
let.push_back('m');
let.push_back('n');
let.push_back('o');
}
else if (i == '7')
{
let.push_back('p');
let.push_back('q');
let.push_back('r');
let.push_back('s');
}
else if (i == '8')
{
let.push_back('t');
let.push_back('u');
let.push_back('v');
}
else if (i == '9')
{
let.push_back('w');
let.push_back('x');
let.push_back('y');
let.push_back('z');
}
return let;
}
};
```
### B
```cpp
class Solution
{
public:
unordered_map map = {{'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"}};
vector res;
void backtrack(string &s, int index, string cur)
{
if (index == s.size())
{
res.push_back(cur);
return;
}
for (int i = 0; i < map[s[index]].size(); ++i)
backtrack(s, index + 1, cur + map[s[index]][i]);
}
vector letterCombinations(string digits)
{
if (digits.size() == 0)
return res;
string cur;
backtrack(digits, 0, cur);
return res;
}
};
```
### C
```cpp
class Solution
{
public:
vector letterCombinations(string digits)
{
if (digits.size() == 0)
return {};
map a;
a.insert(map::value_type('2', "abc"));
a.insert(map::value_type('3', "def"));
a.insert(map::value_type('4', "ghi"));
a.insert(map::value_type('5', "jkl"));
a.insert(map::value_type('6', "mno"));
a.insert(map::value_type('7', "pqrs"));
a.insert(map::value_type('8', "tuv"));
a.insert(map::value_type('9', "wxyz"));
int count = 1;
for (int i = 0; i < digits.size(); i++)
{
count *= a[digits[i]].size();
}
vector res(count);
count = 1;
for (int i = 0; i < digits.size(); i++)
{
int index = 0;
vector temp(res.begin(), res.begin() + count);
for (int k = 0; k < count; k++)
{
for (auto c : a[digits[i]])
{
res[index] = temp[k] + c;
index++;
}
}
count *= a[digits[i]].size();
}
return res;
}
};
```