您的位置:首页 > 其它

17. Letter Combinations of a Phone Number

2016-08-01 23:50 323 查看
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.



Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Subscribe to see whicstatic map<char,vector<string>> mm;
class Solution {
private:
int n=0;

void init()
{
mm['0']={" "};
mm['1']={"*"};
mm['2']={"a","b","c"};
mm['3']={"d","e","f"};
mm['4']={"g","h","i"};
mm['5']={"j","k","l"};
mm['6']={"m","n","o"};
mm['7']={"p","q","r","s"};
mm['8']={"t","u","v"};
mm['9']={"w","x","y","z"};
}
public:
void backtrack(string digits,vector<string>& r1,int k,string temp)
{
if(temp.size()==digits.size())
{
r1.push_back(temp);
return ;
}
else
{
for(int i=k;i<digits.size();++i)
{
for(int j=0;j<mm[digits[i]].size();++j)
{
backtrack(digits,r1,i+1,temp+mm[digits[i]][j]);
}
}
return ;
}
}
vector<string> letterCombinations(string digits) {
init();
vector<string> r1;
if(digits.empty()) return r1;
backtrack(digits,r1,0,"");
return r1;
}
};

h companies asked this question
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: