-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy path0017_letter_combinations_of_a_phone_numbe.cpp
65 lines (58 loc) · 2.02 KB
/
0017_letter_combinations_of_a_phone_numbe.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
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
/*
* Copyright(c) 2021 Jiau Zhang
* For more information see <https://github.com/JiauZhang/algorithms>
*
* This repo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with THIS repo. If not, see <http://www.gnu.org/licenses/>.
*/
/*
https://leetcode-cn.com/problems/3sum-closest
题目描述:
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按任意顺序返回
解题思路:
根据题意肯定是递归遍历所有组合了,关键是现在给定数字个数是不定的,递归的深度就依赖于数字字符串的长度
然后就是局部维护一个字符串作为下一次递归的前缀,知道遍历到终止条件即完成了一个字符组合
*/
class Solution {
public:
const vector<string> maps = {
"",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz",
};
vector<string> letterCombinations(string digits) {
if (digits.size() == 0)
return {};
vector<string> res;
make_string(res, digits, "", 0);
return res;
}
void make_string(vector<string> &res, string &digits, string prefix, int start) {
if (start == digits.size()) {
res.push_back(prefix);
} else {
string cur_string = maps[digits[start] - '0'];
for (auto c: cur_string) {
prefix.push_back(c);
make_string(res, digits, prefix, start+1, end);
prefix.pop_back();
}
}
}
};