链接:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
递归算法:
class Solution
{
public:
vector<string> letterCombinations(string digits)
{
vector<string> ans;
string table[]={"0","0","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
dfs(digits,"",0,table,ans);
return ans;
}
void dfs(string digits,string c,int offset,string table[],vector<string> &ans)
{
if(offset>=digits.length())
{
ans.push_back(c);
return;
}
int pos=digits[offset]-'0';
for(int i=0;i<table[pos].length();i++)
{
string tem=c;
tem+=table[pos].at(i);
dfs(digits,tem,offset+1,table,ans);
tem.clear();
}
}
};