forked from changqing16/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17_c.cpp
More file actions
46 lines (44 loc) · 1.02 KB
/
17_c.cpp
File metadata and controls
46 lines (44 loc) · 1.02 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
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
vector<string> letterCombinations(string digits)
{
queue<string> ans;
vector<string> tmp;
if (digits.size() == 0)
return tmp;
string mapping[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
ans.push("");
for (int i = 0; i < digits.length(); i++)
{
int x = digits[i] - '0';
while (ans.front().length() == i)
{
string tmp = ans.front();
ans.pop();
for (int j = 0; j < mapping[x].length(); j++)
{
ans.push(tmp + mapping[x][j]);
}
}
}
vector<string> last(ans.size());
int len = 0;
while (!ans.empty())
{
last[len++] = ans.front();
ans.pop();
}
return last;
}
int main()
{
string question = "2345";
vector<string> ans = letterCombinations(question);
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
}