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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
这题比较简单,注意细节,我在细节上犯错误了,导致拖了接近半个小时。
class Solution {
public:
void combines(vector<string> &res, vector<string> &map, string str, string left_digits) {
if(left_digits == "") {
res.push_back(str);
return ;
}
char c = left_digits[0];
string tmp = map[atoi(&c)];
string leftd = left_digits.erase(0, 1);
for(int j=0; j<tmp.size(); j++) {
combines(res, map, str+tmp[j], leftd);
}
}
vector<string> letterCombinations(string digits) {
vector<string> map = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> res;
if(digits != "")
combines(res, map, "", digits);
return res;
}
};