
class Solution {
public:
vector<string> res;
unordered_map<char,string> map;
string str = "";
vector<string> letterCombinations(string digits) {
int len = digits.length();
if(len==0)
return res;
// 初始化
map.insert({{'2',"abc"}, {'3',"def"}, {'4',"ghi"}, {'5',"jkl"},
{'6',"mno"}, {'7',"pqrs"}, {'8',"tuv"}, {'9', "wxyz"}});
//map.insert({'2',"abc"}); 单个初始化
// 还可以通过数组形式进行初始化
//map['a'] = "abc";
backbone(digits,0);
return res;
}
void backbone(string digits, int index){
if(index == digits.size()){
res.push_back(str);
}
string tmp = map[digits[index]];
for(int i = 0; i < tmp.size(); i++){
str += tmp[i];
backbone(digits, index+1); // 递归,注意index+1,一下层要处理下一个数
str.pop_back(); // 回溯
}
}
};