leetcode131:letter-combinations-of-a-phone-number
阿新 • • 發佈:2020-08-01
題目描述
給出一個僅包含數字的字串,給出所有可能的字母組合。 數字到字母的對映方式如下:(就像電話上數字和字母的對映一樣)Input:Digit string "23"Output:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].注意:雖然上述答案是按字典序排列的,但你的答案可以按任意的順序給出
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.示例1
輸入
複製"23"
輸出
複製["ad","ae","af","bd","be","bf","cd","ce","cf"]
class Solution {private:
map<char,string> mp={{'2',"abc"},{'3',"def"},{'4',"ghi"},{'5',"jkl"},{'6',"mno"},{'7',"pqrs"},{'8',"tuv"},{'9',"wxyz"}};
vector<string> res;
string temp;
public:
void combine(string &digits,int start){//回溯法
if(start==digits.size()){
res.push_back(temp);return;
}
for(int i=0;i<mp[digits[start]].size();i++){temp+=mp[digits[start]][i];
combine(digits,start+1);
temp.pop_back();
}
}
vector<string> letterCombinations(string digits) {
combine(digits,0);
return res;
}
};