原题目:
使用回溯 + 递归
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
int size = digits.size();
if(size == 0) return res;
string str;
help(digits,str,0,res);
return res;
}
void help(string &digits,string &str,int index,vector<string>&res)
{
if(index == digits.size())
{res.push_back(str);
return;}
string base[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
for (int i = 0; i < base[digits[index] - '0'].size(); i++)
{
str += base[digits[index] - '0'][i];
help(digits, str, index + 1, res);
str.pop_back(); //使用回溯
}
}
};