-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCombinationsofaPhoneNumber.cpp
More file actions
59 lines (51 loc) · 1.26 KB
/
LetterCombinationsofaPhoneNumber.cpp
File metadata and controls
59 lines (51 loc) · 1.26 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
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> letterCombinations(string digits) {
string num[10]={" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string> letters;
vector<string> numofletter;
int sum=1;
int n=digits.length();
if(n==0){
return letters;
}
for(int i=0;i<n;i++){
numofletter.push_back(num[digits[i]-'0']);
sum*=num[digits[i]-'0'].length();
}
int sub;
for(int i=0;i<sum;i++){
string letter="";
int j;
for(j=0;j<n-1;j++){
sub=i;
for(int k=n-1;k>=j+1;k--){
sub/=numofletter.at(k).length();
}
cout<<"sub: "<<sub<<"---";
char ch=numofletter[j][sub%numofletter[j].length()];
cout<<"char: "<<ch<<endl;
letter.append(1,ch);
}
cout<<"mode: "<<numofletter[j].length()<<endl;
char ch=numofletter[j][i%numofletter[j].length()];
letter.append(1,ch);
letters.push_back(letter);
}
return letters;
}
};
int main(){
string digits;
cin>>digits;
Solution *solution=new Solution();
vector<string> letters=solution->letterCombinations(digits);
for(int i=0;i<letters.size();i++){
cout<<letters.at(i)<<endl;
}
return 0;
}