-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution017.cpp
More file actions
47 lines (45 loc) · 971 Bytes
/
solution017.cpp
File metadata and controls
47 lines (45 loc) · 971 Bytes
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
/**
* Letter Combinations of a Phone Number
* DFS
*
* cpselvis([email protected])
* August 19, 2016
*/
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ret;
if (digits.size() == 0)
{
return ret;
}
ret.push_back("");
string letterMap[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (int i = 0; i < digits.size(); i ++)
{
string letters = letterMap[digits[i] - '0'];
vector<string> tmp;
for (int j = 0; j < letters.size(); j ++)
{
for (int k = 0; k < ret.size(); k ++)
{
tmp.push_back(ret[k] + letters[j]);
}
}
ret = tmp;
}
return ret;
}
};
int main(int argc, char **argv)
{
Solution s;
vector<string> ret = s.letterCombinations("23");
for (int i = 0; i < ret.size(); i ++)
{
cout << ret[i] << endl;
}
}