-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path017-2.cpp
37 lines (35 loc) · 1.18 KB
/
017-2.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
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits.size() == 0) return res;
string local;
vector<string> table(2, "");
table.push_back("abc");
table.push_back("def");
table.push_back("ghi");
table.push_back("jkl");
table.push_back("mno");
table.push_back("pqrs");
table.push_back("tuv");
table.push_back("wxyz");
// 使用回溯法
backtracking(table, res, local, 0, digits);
return res;
}
// 关键之一,res为引用,方便后面加值
void backtracking(const vector<string> &table, vector<string> &res, string local, int index, string digits)
{
// 到达枚举的终点,要将东西加入到 结果中去
if(index == digits.size())
res.push_back(local);
for(int i=0;i<table[digits[index] - '0'].size();++i)
{
local.push_back(table[digits[index] - '0'][i]);
// 不断进行尝试
backtracking(table, res, local, index+1, digits);
// 尝试以后要删除
local.pop_back();
}
}
};