LeetCode 17. 电话号码的字母组合
原题链接
中等
作者:
ShineShaye
,
2022-07-04 10:45:37
,
所有人可见
,
阅读 111
class Solution {
public:
string str[10] = {
"", "", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"
};
vector<string> res;
vector<string> letterCombinations(string digits) {
if(digits.empty()) return res;
dfs(digits, 0, "");
return res;
}
void dfs(string& digits, int n, string path)
{
if(n == digits.size())
{
res.push_back(path);
return;
}
int idx = digits[n] - '0';
for(int i = 0; i < str[idx].size(); ++i)
dfs(digits, n + 1, path + str[idx][i]);
}
};