-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.cpp
More file actions
54 lines (53 loc) · 934 Bytes
/
17.cpp
File metadata and controls
54 lines (53 loc) · 934 Bytes
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
using namespace std;
class Solution {
private:
const string letterMap[10]={
" ", //0
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
vector<string> res;
void findCombination(const string &digits,int index,const string &s)
{
//digits:要处理的数字字符串。index:数位 s:之前数字转换的字母字符串
//s:从digits[0,n-1]
if(index==digits.size())
{
//保存s
res.push_back(s);
return;
}
char c = digits[index];
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;
}
};