17. 电话号码的字母组合
思路:典型的深度搜索dfs,特别注意点就是字符串为空的时候什么都不输出
class Solution {
public:
vector<char> mp[15]={{},{},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
vector<string> v;
void dfs(string digits, string s){
if(digits.size()==0){
v.push_back(s);
return ;
}
for(int i=0;i<mp[digits[0]-'0'].size();i++){
dfs(digits.substr(1),s+mp[digits[0]-'0'][i]);
}
}
vector<string> letterCombinations(string digits) {
//注意字符串为空
if(digits.size())
dfs(digits,"");
return v;
}
};