-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path17.cpp
More file actions
35 lines (33 loc) · 1012 Bytes
/
Copy path17.cpp
File metadata and controls
35 lines (33 loc) · 1012 Bytes
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
class Solution {
public:
vector<string> letterCombinations(string digits) {
int n = digits.size();
if(n == 0) return vector<string>();
char a = 'a';
for (int i = 0; i < 8; ++i) {
tran.push_back(vector<char>());
for (int j = 0; j < 3; ++j)
tran[i].push_back(a++);
if(i == 5)
tran[i].push_back(a++);
}
tran[7].push_back(a);
string temp;
vector<string> ret;
BackTracking(digits, n, 0, temp, ret);
return ret;
}
void BackTracking(string digits, int &n, int i, string &temp, vector<string> &ret) {
if (i == n) {
ret.push_back(temp);
return;
}
for (int j = 0; j < (tran[digits[i] - '0' - 2]).size(); ++j) {
temp.push_back(tran[digits[i] - '0' - 2][j]);
BackTracking(digits, n, i + 1, temp, ret);
temp.pop_back();
}
}
private:
vector<vector<char>> tran;
};