-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLetterCombinationsOfAPhoneNumber.h
85 lines (83 loc) · 1.85 KB
/
LetterCombinationsOfAPhoneNumber.h
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
/**
*Solution 1
*recursive
*time:O(3^n) space:O(n)
*/
class Solution {
public:
vector<string> letterCombinations(string digits) {
string s("");
if(digits.empty())
{
res.push_back(s);
return res;
}
vs.push_back("abc");
vs.push_back("def");
vs.push_back("ghi");
vs.push_back("jkl");
vs.push_back("mno");
vs.push_back("pqrs");
vs.push_back("tuv");
vs.push_back("wxyz");
dfs(s,digits,0);
return res;
}
private:
vector<string> vs;
vector<string> res;
void dfs(string &s,string d,int n)
{
if(n==d.size())
{
res.push_back(s);
return;
}
if(d[n]>'1'&&d[n]<='9')
{
string str(vs[d[n]-'2']);
for(auto i=begin(str);i!=end(str);++i)
{
s.push_back(*i);
dfs(s,d,n+1);
s.pop_back();
}
return;
}
dfs(s,d,n+1);
return;
}
};
/**
*Solution 2
*iterative,神迭代
*time:O(3^n) space:O(1)
*/
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ret(1,"");
for(int i=0;i<digits.size();++i)
{
int sz=ret.size();
for(int j=0;j<sz;++j)
{
const string &s(v[digits[i]-'2']);
for(int k=0;k<s.size();++k)
{
if(k!=s.size()-1)
{
ret.push_back(ret[j]+s[k]);
}
else
{
ret[j]+=s[k];
}
}
}
}
return ret;
}
private:
vector<string> v={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
};