-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLeetcode_letter-combinations-of-a-phone-number.cc
99 lines (98 loc) · 2.38 KB
/
Leetcode_letter-combinations-of-a-phone-number.cc
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Solution {
private:
map<char, vector<char>>dm;
public:
void dfs(vector<string>&ans, string str, string digits)
{
if(digits.empty())
{
ans.push_back(str);
return;
}
char ch = digits[0];
for(int i = 0; i < dm[ch].size(); ++i)
{
str += dm[ch][i];
dfs(ans, str, digits.substr(1));
str = str.substr(0, str.length()-1);
}
}
vector<string> letterCombinations(string digits) {
vector<string>ans;
if(digits.empty())
{
ans.push_back(digits);
return ans;
}
int st = 0, ed = 0;
dm.clear();
for(char i = 2; i<=9; ++i)
{
if(i==7 || i==9)
ed += 4;
else
ed += 3;
for(int j = 0; j<ed-st; ++j)
dm[i+'0'].push_back(st+'a'+j);
st = ed;
}
dfs(ans, "", digits);
}
};
//SECOND TRIAL
class Solution {
private:
vector<string> strvec;
void dfs(vector<string>&ans, string& str, string digits)
{
if(digits.empty())
{
ans.push_back(str);
return;
}
if(digits[0]<'2')
return;
string numstr = strvec[digits[0]-'2'];
for(int i = 0; i<numstr.length(); ++i)
{
str += numstr[i];
dfs(ans, str, digits.substr(1));
str=str.substr(0, str.length()-1);
}
};
public:
vector<string> letterCombinations(string digits) {
vector<string>ans;
if(digits.empty())
{
ans.push_back(digits);
return ans;
}
string num, str;
for(int i = 2; i<=9; ++i)
{
num = "";
if(i==7 || i==9)
{
num += strvec.back().back()+1;
for(int i = 0; i<=2; ++i)
num += num[i]+1;
strvec.push_back(num);
}
else
{
if(i==2)
strvec.push_back("abc");
else
{
num += strvec.back().back()+1;
num += num[0]+1;
num += num[1]+1;
strvec.push_back(num);
}
}
}
dfs(ans, str, digits);
return ans;
}
};