leetcode链接:https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/
代码:
class Solution {
public:
const char * letterCombine(int i) {
static const char *letter[] = { "" , "" , "abc" , "def" , "ghi" , "jkl" , "mno" , "pqrs" , "tuv" , "wxyz" };
return letter[i];
}
void combine(vector<string>& v, int i, string curCombine, string& digits) {
if (i == digits.size()) {
v.push_back(curCombine); // 添加
return;
}
int num = digits[i] - '0';
string s = letterCombine(num);
for (auto val : s) {
combine(v, i + 1, curCombine + val, digits);
}
}
vector<string> letterCombinations(string digits) {
vector<string> v;
if (digits.empty()) // 空直接返回
return v;
string curCombine; // 用来存储当前组合
combine(v, 0, curCombine, digits);
return v;
}
};