题目链接:17. 电话号码的字母组合 - 力扣(LeetCode)
代码如下:
class Solution {
private:const string letterMap[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
public:vector<string> result;string s;void backtracking(const string& digits, int index){if(index == digits.size()){result.push_back(s);return ;}int digit = digits[index] - '0';string Letter = letterMap[digit];for(int i = 0; i < Letter.size(); i++){s.push_back(Letter[i]);backtracking(digits, index + 1);s.pop_back();}}vector<string> letterCombinations(string digits) {s.clear();result.clear();if(digits.size() == 0){return result;}backtracking(digits, 0);return result;}
};
这个题目也就是先使用一个映射定义好字符与数字的映射关系,然后再根据回溯三部曲来去写这个题目。