题目链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
题目描述:
示例 1:
输入:digits = "23"
输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
示例 2:
输入:digits = ""
输出:[]
示例 3:
输入:digits = "2"
输出:["a","b","c"]
题解:
class Solution {
public:
unordered_map<char, string> phoneMap
{
{'2', "abc"},
{'3', "def"},
{'4', "ghi"},
{'5', "jkl"},
{'6', "mno"},
{'7', "pqrs"},
{'8', "tuv"},
{'9', "wxyz"}
};
vector<string> result;
string s;
void backtracking(const string& digits, int index)
{
if(index == digits.size())
{
result.push_back(s);
return;
}
string letters = phoneMap[digits[index]];
for(int i = 0; i < letters.size(); i++)
{
s.push_back(letters[i]);
backtracking(digits, index + 1);
s.pop_back();
}
}
vector<string> letterCombinations(string digits) {
if(digits.size() == 0)
return result;
backtracking(digits, 0);
return result;
}
};