class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ans;
if(digits.size() == 0)
return ans;
int depth = digits.size();
string tmp(depth, 0);
dfs(tmp, 0, depth, ans, digits);
return ans;
}
void dfs(string &tmp, int curdep, int depth, vector<string> &ans, string &digits){
if(curdep >= depth){
ans.push_back(tmp);
return ;
}
for(int i = 0; i < dic[digits[curdep] - '0'].size(); ++ i){
tmp[curdep] = dic[digits[curdep] - '0'][i];
dfs(tmp, curdep + 1, depth, ans, digits);
}
return ;
}
private:
string dic[10] = {{""},{""},{"abc"},{"def"},{"ghi"},{"jkl"},{"mno"},{"pqrs"},{"tuv"},{"wxyz"}};
};
采用深度优先搜索(dfs),递归调用。