电话号码的数字组合
程序员文章站
2024-01-24 17:34:16
...
string a[] = {
{" "},
{" "},
{"abc"},
{"def"},
{"ghi"},
{"jkl"},
{"mon"},
{"pqrs"},
{"tuv"},
{"wxyz"}
};
vector<string> res;
class Solution {
public:
vector<string> letterCombinations(string digits) {
res.clear();
if (digits.size() == 0)
return res;
else
{
string s = "";
dfs(digits, 0, s);
return res;
}
}
void dfs(string digits, int index, string s)
{
if (index >= digits.size())
{
res.push_back(s);
return;
}
for (int i = index; i < digits.size(); i++)
{
int m = digits[i] - '0';
for (int j = 0; j < a[m].size(); j++)
{
s += a[m][j];
dfs(digits, index+1,s);
s.replace(s.size() - 1, 1, "");
}
}
}
};
上一篇: 数据结构(栈和队列的链表方式实现)
下一篇: 机器学习——【2】特征工程