给定一个仅包含数字 2-9
的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
三个for循环暴力字典 c++ code :AC 100%
class Solution {
public:
vector<string> letterCombinations(string digits) {
//题目说的2-9
vector<string>s1, s2;
map<char, vector<string>>mp;
mp['2'] = { "a", "b", "c" };
mp['3'] = { "d", "e", "f" };
mp['4'] = { "g", "h", "i" };
mp['5'] = { "j", "k", "l" };
mp['6'] = { "m", "n", "o" };
mp['7'] = { "p", "q", "r", "s" };
mp['8'] = { "t", "u", "v" };
mp['9'] = { "w", "x", "y", "z" };
for (auto &ch : digits)
{
int size = s1.size();
auto &v = mp[ch];
for (auto &s : v)
{
for (int i = 0; i < size; i++)
{
string temp = s1[i] + s;
s2.push_back(temp);
}
if (size == 0)s2.push_back(s);
}
swap(s1, s2);
s2.clear();
}
return s1;
}
};