-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path425.h
46 lines (44 loc) · 1.22 KB
/
425.h
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
class Solution {
public:
/**
* @param digits: A digital string
* @return: all posible letter combinations
*/
vector<string> letterCombinations(string &digits) {
// write your code here
vector<string> v;
string tmp;
letter(digits, 0, tmp, v);
return v;
}
private:
void letter(string &digits, int start, string &str, vector<string> &v){
if(start == digits.size()){
if(str.size() != 0 && str.size() == digits.size())
v.push_back(str);
return;
}
for(int i = start; i < digits.size(); i++){
int index = digits[i] - '0';
if(index == 0 || index == 1) continue;
for(int j = 0; j < 4 && digit[index][j] ; j++){
string oldstr = str;
str += digit[index][j];
letter(digits, i+1, str, v);
str = oldstr;
}
}
}
const char digit[10][5] = {
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz" // 9
};
};