-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLetter-Combinations-of-a-Phone-Number.cpp
58 lines (54 loc) · 1.23 KB
/
Letter-Combinations-of-a-Phone-Number.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<string> res;
vector<char> phonenum_to_char(int x)
{
switch (x)
{
case 2:
return {'a', 'b', 'c'};
case 3:
return {'d', 'e', 'f'};
case 4:
return {'g', 'h', 'i'};
case 5:
return {'j', 'k', 'l'};
case 6:
return {'m', 'n', 'o'};
case 7:
return {'p', 'q', 'r', 's'};
case 8:
return {'t', 'u', 'v'};
case 9:
return {'w', 'x', 'y', 'z'};
default:
return {};
};
}
void dfs(vector<vector<char>> &v, int n, string cur, int i)
{
if (i == n)
{
if (cur != "")
res.push_back(cur);
return;
}
for (int j = 0; j < v[i].size(); j++)
{
dfs(v, n, cur + v[i][j], i + 1);
};
return;
}
vector<string> letterCombinations(string digits)
{
int n = digits.length();
vector<vector<char>> v(n);
for (int i = 0; i < n; i++)
v[i] = phonenum_to_char(digits[i] - '0');
dfs(v, n, "", 0);
return res;
}
};