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:
vector<string> letterCombinations(string digits) {
vector<string> ret;
vector<string> Num = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
string temp;
if(digits.size()==0)
return ret;
Dfs(digits,ret,Num,temp,0);
return ret;
}
void Dfs(string digits,vector<string> &ret,vector<string>Num,string temp,int index){
if(index==digits.size()){
ret.push_back(temp);
return;
}
int digit = digits[index]-'2';
for(int i=0;i<Num[digit].size();++i ){
temp.push_back(Num[digit][i]);
Dfs(digits,ret,Num,temp,index+1);
temp.pop_back();
}
}
};