Letter Combinations of a Phone Number
2014-05-26 22:38
411 查看
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.
class Solution {
public:
void dfs(string table[],vector<string>&vec,int dep, int max, string &s, string cur){
string str = table[s.at(dep) - '2'];
for(int i = 0; i < str.length(); i++){
string next = cur + str.at(i);
if(dep == max - 1){
vec.push_back(next);
}
else{
dfs(table,vec,dep + 1, max, s, next);
}
}
}
vector<string> letterCombinations(string digits) {
vector<string>vec;
if(digits.length() <= 0){
vec.push_back("");
return vec;
}
string table[8] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
dfs(table,vec,0,digits.size(),digits,"");
return vec;
}
};
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"].
class Solution {
public:
void dfs(string table[],vector<string>&vec,int dep, int max, string &s, string cur){
string str = table[s.at(dep) - '2'];
for(int i = 0; i < str.length(); i++){
string next = cur + str.at(i);
if(dep == max - 1){
vec.push_back(next);
}
else{
dfs(table,vec,dep + 1, max, s, next);
}
}
}
vector<string> letterCombinations(string digits) {
vector<string>vec;
if(digits.length() <= 0){
vec.push_back("");
return vec;
}
string table[8] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
dfs(table,vec,0,digits.size(),digits,"");
return vec;
}
};
相关文章推荐
- Letter Combinations of a Phone Number
- leetcode 17. Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- [LeetCode]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
- 13 - Letter Combinations of a Phone Number
- 17. Letter Combinations of a Phone Number**
- [leetcode] Letter Combinations of a Phone Number
- LeetCode Online Judge 题目C# 练习 - Letter Combinations of a Phone Number
- Binary Tree Zigzag Level Order Traversal & Partition List & Letter Combinations of a Phone Number
- Leetcode Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number
- 手机键盘上的排列组合问题 Letter Combinations of a Phone Number
- leetcode 第17题 Letter Combinations of a Phone Number
- [leetcode]Letter Combinations of a Phone Number
- LeetCode Letter Combinations of a Phone Number 电话号码组合
- Letter Combinations of a Phone Number解题报告
- Leetcode: Letter Combinations of a Phone Number