典型的dfs题,不多阐述了,记得特判输入数字串为空的情况就可以了。
class Solution {
public:
vector<string> ans;
string strs[10] = {
"","","abc","def",
"ghi","jkl","mno",
"pqrs","tuv","wxyz",
};
vector<string> letterCombinations(string digits) {
if(digits.empty()) return ans;
dfs(digits,0,"");
return ans;
}
void dfs(string& digits,int u,string path){
if(u == digits.size()) ans.push_back(path);
else {
for(auto c : strs[digits[u] - '0']){
dfs(digits,u+1,path + c);
}
}
}
};