forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
73 lines (55 loc) · 1.45 KB
/
main.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
/// Source : https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
/// Author : liuyubobobo
/// Time : 2017-11-18
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
using namespace std;
/// Backtracking
/// Time Complexity: O(2^len(s))
/// Space Complexity: O(len(s))
class Solution {
private:
const string letterMap[10] = {
" ", //0
"", //1
"abc", //2
"def", //3
"ghi", //4
"jkl", //5
"mno", //6
"pqrs", //7
"tuv", //8
"wxyz" //9
};
vector<string> res;
void findCombination(const string &digits, int index, const string &s){
if(index == digits.size()){
res.push_back(s);
return;
}
char c = digits[index];
assert(c >= '0' && c <= '9' && c != '1');
string letters = letterMap[c - '0'];
for(int i = 0 ; i < letters.size() ; i ++)
findCombination(digits, index+1, s + letters[i]);
return;
}
public:
vector<string> letterCombinations(string digits) {
res.clear();
if(digits == "")
return res;
findCombination(digits, 0, "");
return res;
}
};
void printVec(const vector<string>& vec){
for(string s: vec)
cout << s << endl;
}
int main() {
printVec(Solution().letterCombinations("234"));
return 0;
}