递归穷举
class Solution {
public:
string s[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> res;
vector<string> letterCombinations(string digits)
{
if (digits.length() == 0) return res;
string temp = "";
f(digits, 0, temp);
return res;
}
void f(string digits, int n, string temp)
{
if (n == digits.length())
{
res.push_back(temp);
return;
}
int index = digits[n] - '0';
for (int i = 0; i < s[index].length(); i++)
{
temp += s[index][i];
f(digits, n + 1, temp);
temp.erase(temp.begin() + temp.length() - 1);
}
}
};