题目要求
解题思路
这道题先画出决策树,然后根据决策树使用dfs并回溯即可
代码实现
class Solution
{//数字映射字符串vector<string> hash={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//返回值vector<string> ret;//路径信息string path;
public:vector<string> letterCombinations(string digits) {//dfs+回溯//处理特殊情况if(digits=="") return ret;dfs(digits,0);return ret;}void dfs(string& digits,int pos){//路径到头了if(pos==digits.size()){ret.push_back(path);return ;}//遍历对应位置的字符串for(auto& e:hash[digits[pos]-'0']){path.push_back(e);dfs(digits,pos+1);//回溯,恢复现场path.pop_back();}}
};