-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#18.cc
More file actions
41 lines (35 loc) · 1.04 KB
/
Copy pathLeetCode#18.cc
File metadata and controls
41 lines (35 loc) · 1.04 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
class Solution {
private:
void dfs(string str,int ind, string& digits, map<char,string>& table, vector<string>& ret){
if(ind == digits.length()){
ret.push_back(str);
return;
}
if(digits[ind]=='1') dfs(str,ind+1,digits,table,ret);
else{
string& s = table[digits[ind]];
for(int i=0;i<s.length();i++)
dfs(str+s[i],ind+1,digits,table,ret);
}
}
public:
vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> ret;
map<char,string> table;
table['0']=" ";
table['1']="";
table['2']="abc";
table['3']="def";
table['4']="ghi";
table['5']="jkl";
table['6']="mno";
table['7']="pqrs";
table['8']="tuv";
table['9']="wxyz";
string s = "";
dfs(s,0,digits,table,ret);
return ret;
}
};