class Solution
{
const char* To[10] = { "","", "abc", "def", "ghi",
"jkl", "mno", "pqrs","tuv", "wxyz" };
//常量字符串初始化string 注意此非定义(缺省值)--实例化对象时初始化
//string numToStr[10] = { "","", "abc", "def", "ghi",
// "jkl", "mno", "pqrs","tuv", "wxyz" };
public:
void generate(string digit, int i, vector<string>& Vs, string str)
{
if (i == digit.size()) //如果本次i == size
//表明上次的i已经到达一个字符串的末字符
{
Vs.push_back(str); //转化完毕--保存
return; //返回上一次递归
}
int n = digit[i] - '0'; //字符变数字
string s = To[n]; //数字对应的字符串
for (auto ch : s)
{
generate(digit, i + 1, Vs, str + ch);
}
}
vector<string> letterCombinations(string digit)
{
vector<string> Vs;
string s;
if (digit.empty())
return Vs;
//此时传的 Vs s 均为空对象
//
//Vs的类型是vector<string>
//Vs用来存储多个已转化完毕的字符串的
//
//s的类型是string
//s用来存储每一次转化完毕的字符串
generate(digit, 0,Vs, s);
return Vs;
}
};