-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
68 lines (59 loc) · 1.52 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;
class Solution {
private:
vector<string> result;
string path;
unordered_map<char, string> map{{'2', "abc"}, {'3', "def"}, {'4', "ghi"},
{'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"},
{'8', "tuv"}, {'9', "wxyz"}};
void backtracking(string digits, int index) {
if (index == digits.size()) {
result.push_back(path);
return;
}
for (int i = 0; i < map[digits[index]].size(); i++) {
path += map[digits[index]][i];
backtracking(digits, index + 1);
path = path.substr(0, path.size() - 1);
}
}
public:
vector<string> letterCombinations(string digits) {
if (digits.size() == 0) {
return vector<string>();
}
result.clear();
path = "";
backtracking(digits, 0);
return result;
}
};
void output(const vector<string> &sets, int index) {
cout << "case" << index << ":";
cout << "[";
for (auto i = sets.begin(); i != sets.end(); i++) {
cout << "\""<< *i << "\"";
if (i != (sets.end() - 1)) {
cout << ",";
}
}
cout << "]" << endl;
}
int main(int argc, char *argv[]) {
string s1 = "23";
string s2 = "";
string s3 = "2";
Solution solution;
output(solution.letterCombinations(s1), 1);
output(solution.letterCombinations(s2), 2);
output(solution.letterCombinations(s3), 3);
return 0;
}