-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.letter-combinations-of-a-phone-number.cpp
50 lines (46 loc) · 1.3 KB
/
17.letter-combinations-of-a-phone-number.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
/*
* @lc app=leetcode id=17 lang=cpp
*
* [17] Letter Combinations of a Phone Number
*/
// @lc code=start
class Solution {
private:
vector<vector<char>> dict = {{},
{},
{'a', 'b', 'c'},
{'d', 'e', 'f'},
{'g', 'h', 'i'},
{'j', 'k', 'l'},
{'m', 'n', 'o'},
{'p', 'q', 'r', 's'},
{'t', 'u', 'v'},
{'w', 'x', 'y', 'z'}};
public:
vector<string> letterCombinations(string digits) {
if (digits.length() == 0) return {};
queue<string> q;
q.push("");
int q_size = 1;
for (char c : digits) {
int d = c - '0';
int loop = q_size;
while (loop--) {
string s = q.front();
q.pop();
q_size--;
for (char c : dict[d]) {
q.push(s + c);
q_size++;
}
}
}
vector<string> ans;
while (!q.empty()) {
ans.push_back(q.front());
q.pop();
}
return ans;
}
};
// @lc code=end