-
Notifications
You must be signed in to change notification settings - Fork 0
/
17
53 lines (53 loc) · 1.45 KB
/
17
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
class Solution {
public:
vector<string> letterCombinations(string digits) {
map<char,string> sm;
for(int i = 0, k = 0; i < 8; i++){
string s;
for(int j = 0; j < 3; j++)
s += 'a'+i*3+j+k;
if(i == 7)
s += 'z';
else if(i == 5){
s += 's';
k = 1;
}
sm.insert(pair<char, string>('2' + i, s));
}
vector<string> output;
if(digits.length() == 0)
return output;
vector<int> a(digits.length(), 0);
int ent = 0;
string s0;
for(int k = 0; k != digits.length(); k++ ){
s0 += sm[digits[k]][a[k]];
}
output.push_back(s0);
while(1){
ent = 1;
string s;
for(char j = digits.length() - 1; j != -1; j --){
char i = digits[j];
a[j] += ent;
if(sm[i].length() == a[j]){
a[j] = 0;
ent = 1;
}
else{
ent = 0;
break;
}
}
if(ent)
break;
else{
for(int k = 0; k != digits.length(); k++ ){
s += sm[digits[k]][a[k]];
}
output.push_back(s);
}
}
return output;
}
};