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.
1 - 深度优先搜索,采用递归实现。
class Solution {
public:
vector<string> keyboard = {" ","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string> letterCombinations(string digits) {
vector<string> result;
dfs(digits,result,0,"");
return result;
}
private:
void dfs(string digits,vector<string> &result,int count,string path){
if(digits=="") return;
if(count==digits.size()) {
result.push_back(path);
return;
}
for(int i=0;i<keyboard[digits[count]-'0'].size();i++){
dfs(digits,result,count+1,path+keyboard[digits[count]-'0'][i]);
}
}
};