-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path17.电话号码的字母组合.cpp
51 lines (44 loc) · 1.11 KB
/
17.电话号码的字母组合.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=17 lang=cpp
*
* [17] 电话号码的字母组合
*/
// @lc code=start
class Solution
{
public:
void letter(std::vector<std::string> &result, std::unordered_map<char, std::string> &maps, const std::string &digits, std::string &s, int index)
{
if (digits.size() == s.size())
{
if (s.size() != 0)
{
result.emplace_back(s);
}
return;
}
for (const auto &c : maps[digits[index]])
{
s.push_back(c);
letter(result, maps, digits, s, index + 1);
s.pop_back();
}
}
vector<string> letterCombinations(string digits)
{
std::vector<std::string> result;
std::unordered_map<char, std::string> maps;
std::string s;
maps['2'] = "abc";
maps['3'] = "def";
maps['4'] = "ghi";
maps['5'] = "jkl";
maps['6'] = "mno";
maps['7'] = "pqrs";
maps['8'] = "tuv";
maps['9'] = "wxyz";
letter(result, maps, digits, s, 0);
return result;
}
};
// @lc code=end