-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
letterCombinationsOfAPhoneNumber.cpp
91 lines (84 loc) · 2.64 KB
/
letterCombinationsOfAPhoneNumber.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
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
// Source : https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
// Author : Hao Chen
// Date : 2014-07-17
/**********************************************************************************
*
* Given a digit string, return all possible letter combinations that the number could represent.
*
* A mapping of digit to letters (just like on the telephone buttons) is given below.
*
* Input:Digit string "23"
* Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
*
* Note:
* Although the above answer is in lexicographical order, your answer could be in any order you want.
*
*
**********************************************************************************/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<string> letterCombinations(string digits) {
char phone[10][4]={ {' ', '\0', '\0', '\0' }, //0
{'\0', '\0', '\0', '\0' }, //1
{'a', 'b', 'c', '\0' }, //2
{'d', 'e', 'f', '\0' }, //3
{'g', 'h', 'i', '\0' }, //4
{'j', 'k', 'l', '\0' }, //5
{'m', 'n', 'o', '\0' }, //6
{'p', 'q', 'r', 's' }, //7
{'t', 'u', 'v', '\0' }, //8
{'w', 'x', 'y', 'z' } //9
};
vector<string> result;
if (digits.size()<=0){
result.push_back("");
return result;
}
for( int i=0; i<digits.size(); i++ ) {
if (!isdigit(digits[i])) {
vector<string> r;
return r;
}
int d = digits[i] - '0';
if (result.size()<=0){
for( int j=0; j<4 && phone[d][j]!='\0'; j++ ){
string s;
s += phone[d][j];
result.push_back(s);
}
continue;
}
vector<string> r;
for (int j=0; j<result.size(); j++){
for( int k=0; k<4 && phone[d][k]!='\0'; k++ ){
string s = result[j] + phone[d][k];
//sort(s.begin(), s.end());
r.push_back(s);
}
}
result = r;
}
//sort(result.begin(), result.end());
return result;
}
void printVector(vector<string>& ss){
cout << "{ ";
for(int i=0; i<ss.size(); i++){
if (i>0) cout << ", ";
cout << ss[i];
}
cout << " }" << endl;
}
int main(int argc, char**argv)
{
string s="23";
if (argc>1){
s=argv[1];
}
vector<string> ss = letterCombinations(s);
printVector(ss);
return 0;
}