这样的题目其实就是提多叉树的遍历,一直向下,然后从左向右遍历。
class Solution {
string arr[10]={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};//把所有的组合列出来
public:
void _letterCombinations(const string& digits,size_t i,string Cominstr,vector<string>& strV)
{ if(i==digits.size())
{
strV.push_back(Cominstr);//进行返回条件
return;
}
string str1=arr[digits[i]-'0'];//像下递归的条件
for(int j=0;j<str1.size();++j)
{
_letterCombinations(digits,i+1,Cominstr+str1[j],strV);//从左向右递归
}
}
vector<string> letterCombinations(string digits) //主的递归函数
{
vector<string>ans;
string Cominstr;
if(digits.empty())
{
return ans;
}
_letterCombinations(digits,0,Cominstr,ans);
return ans;
}
};