class Solution {
private:
void solve(vector<string> &result,string &cur,int level,vector<vector<char>> table,string &digits)
{
if(level==digits.length())
{
result.push_back(cur);
return;
}
else
{
int x=digits[level]-'0';
for(int i=0;i<table[x].size();i++)
{
cur.push_back(table[x][i]);
solve(result,cur,level+1,table,digits);
cur.pop_back();
}
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
if(digits.length()==0)
return result;
string cur="";
vector<vector<char>> table(2,vector<char>());
table.push_back(vector<char>{'a','b','c'});// index 2
table.push_back(vector<char>{'d','e','f'});// 3
table.push_back(vector<char>{'g','h','i'});//4
table.push_back(vector<char>{'j','k','l'});// 5
table.push_back(vector<char>{'m','n','o'});//6
table.push_back(vector<char>{'p','q','r','s'});// 7
table.push_back(vector<char>{'t','u','v'});//8
table.push_back(vector<char>{'w','x','y','z'});// 9
solve(result,cur,0,table,digits);
return result;
}
};
17. Letter Combinations of a Phone Number
最新推荐文章于 2022-04-25 11:56:55 发布