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.
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
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
相关文章推荐
- 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- leetcode 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number**
- LeetCode 17.Letter Combinations of a Phone Number
- leetcode --17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- LeetCode 简单操作 | 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- leetcode--17. Letter Combinations of a Phone Number
- leetcode 17. Letter Combinations of a Phone Number
- LeetCode 17.Letter Combinations of a Phone Number & 19.Remove Nth Node From End of List
- leetcode-17. Letter Combinations of a Phone Number
- LeetCode | 17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number
- leetcode--17. Letter Combinations of a Phone Number
- Leetcode:17. Letter Combinations of a Phone Number
- LeetCode-17. Letter Combinations of a Phone Number
- 【leetcode】17. Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number