-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathp017.cpp
More file actions
22 lines (19 loc) · 749 Bytes
/
p017.cpp
File metadata and controls
22 lines (19 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
const char d2s[10][6] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
const unsigned len[10] = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};
void f(unsigned pos, string &digits, vector<string> &result, string curstring)
{
for (int i = 0; i < len[digits[pos]-'0']; i++)
{
if (pos == digits.length()-1) result.push_back(curstring+d2s[digits[pos]-'0'][i]);
else f(pos+1, digits, result, curstring+d2s[digits[pos]-'0'][i]);
}
}
vector<string> letterCombinations(string digits) {
vector<string> result;
if (digits.length() == 0) return result;
f(0, digits, result, "");
return result;
}
};