class Solution {
//static declare and definition with local scope
public:
void letterCombinations_aux(int step, string& path, vector<string>& ans, const string& digits)
{
//pay attention to this kind of statement
const static string strT[10] = {"","","abc","def","ghi","jkl","mno","qprs","tuv","wxyz"};
if(step == digits.size())
{
ans.push_back(path);
return;
}
for(int i = 0; i < strT[digits[step]-'0'].size(); ++i)
{
path.push_back(strT[digits[step]-'0'][i]);
letterCombinations_aux(step+1, path, ans, digits);
path.pop_back();
}
}
vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string path;
vector<string> ans;
int step = 0;
letterCombinations_aux(step, path, ans, digits);
return ans;
}
};[LeetCode]Letter Combinations of a Phone Number
最新推荐文章于 2020-08-10 10:20:18 发布
本文介绍了一个使用C++实现的电话号码对应的字母组合生成器。通过递归辅助函数完成从数字到预定义字母的映射,实现了所有可能的字符串组合。文章详细展示了如何根据输入的数字序列生成所有可能的字母组合。
568

被折叠的 条评论
为什么被折叠?



