-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17+Letter Combinations of a Phone Number.cpp
More file actions
85 lines (66 loc) · 2.09 KB
/
Copy path17+Letter Combinations of a Phone Number.cpp
File metadata and controls
85 lines (66 loc) · 2.09 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Solution {
public:
vector<string> res;
string resT;
string a[10] = {"",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"};
void dfs(string &digits, int start){
if (resT.size() == digits.size()) {
res.push_back(resT);
return;
}
for (int i = start; i < digits.size(); ++i) {
int number = digits[i] - '0';
for (int j = 0; j < a[number].size(); ++j) {
char c = a[number][j];
resT.push_back(c);
dfs(digits, i+1);
resT.pop_back();
}
}
return;
}
vector<string> letterCombinations(string digits) {
if (digits.size() == 0) return res;
dfs(digits, 0);
return res;
}
};
class Solution {
public:
vector<string> res;
string resT;
char a[10][4] = {{}, {}, {'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'},
{'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'} };
void dfs(string &digits, int start){
if (resT.size() == digits.size()) {
res.push_back(resT);
return;
}
for (int i = start; i < digits.size(); ++i) {
int number = digits[i] - '0';
for (int j = 0; j <= 3; ++j) {
char c = a[number][j];
if (c != '\000') { //char数组 没有初始化均为\000
resT.push_back(c);
dfs(digits, i+1);
resT.pop_back();
}
}
}
return;
}
vector<string> letterCombinations(string digits) {
if (digits.size() == 0) return res;
dfs(digits, 0);
return res;
}
};