题目
Number: 17
Difficulty: Medium
Tags: Backtracking, String
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input: Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
题解
模拟手机9键输入字母,给出输入的数字,输出所有可能的字母组合。
类似于笛卡尔积。
代码
非递归:
vector<string> letterCombinations(string digits) {
vector<string> result;
if(digits.empty())
return result;
string buttons[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
result.push_back("");
for(int i = 0; i < digits.size(); ++i)
{
vector<string> temp;
string ch = buttons[digits[i] - '0'];
for(int j = 0; j < ch.size(); ++j)
for(int k = 0; k < result.size(); ++k)
temp.push_back(result[k] + ch[j]);
result = temp;
}
return result;
}
回溯的方法:
/* BackTracking */
vector<string> letterCombinations(string digits){
vector<string> result;
if(digits.empty())
return result;
string buttons[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> temp = letterCombinations(digits.substr(1));
string ch = buttons[digits[0] - '0'];
for(int i = 0; i < ch.size(); ++i)
{
if(temp.empty())
result.push_back(string(1, ch[i]));
else
for(int j = 0; j < temp.size(); ++j)
result.push_back(ch[i] + temp[j]);
}
return result;
}
总结
char to string:
string s(1, c);
std::cout << s << std::endl;
and
std::cout << string(1, c) << std::endl;
and
string s;
s.push_back(c);
std::cout << s << std::endl;