forked from pengsida/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode17.cpp
39 lines (36 loc) · 910 Bytes
/
leetcode17.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
class Solution {
private:
vector<string> rec = vector<string>(10);
string word;
vector<string> ans;
public:
Solution() {
rec[2] = "abc";
rec[3] = "def";
rec[4] = "ghi";
rec[5] = "jkl";
rec[6] = "mno";
rec[7] = "pqrs";
rec[8] = "tuv";
rec[9] = "wxyz";
}
void _combination(string& digits, int pos) {
if (pos < 0) {
ans.push_back(word);
return;
}
auto& letters = rec[digits[pos] - '0'];
for (auto letter : letters) {
word[pos] = letter;
_combination(digits, pos - 1);
}
}
vector<string> letterCombinations(string digits) {
if (digits.empty())
return ans;
int size = digits.length();
word = string(size, ' ');
_combination(digits, size - 1);
return ans;
}
};