17. Letter Combinations of a Phone Number
2017-08-08 20:13
387 查看
题目:
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:
map<int,string> m={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};
vector<string> letterCombinations(string digits) {
vector<string> res;
string temp;
if(digits.empty())
return res;
solve(digits,0,res,temp);
return res;
}
void solve(string digits,int index,vector<string> &res,string temp){
if(index==digits.size())
res.push_back(temp);
int i=digits[index]-'0';
for(int j=0;j<m[i].size();j++){
solve(digits,index+1,res,temp+m[i][j]);
}
}
};
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"].
枚举递归
class Solution {
public:
map<int,string> m={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};
vector<string> letterCombinations(string digits) {
vector<string> res;
string temp;
if(digits.empty())
return res;
solve(digits,0,res,temp);
return res;
}
void solve(string digits,int index,vector<string> &res,string temp){
if(index==digits.size())
res.push_back(temp);
int i=digits[index]-'0';
for(int j=0;j<m[i].size();j++){
solve(digits,index+1,res,temp+m[i][j]);
}
}
};
相关文章推荐
- 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
- 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
- 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
- leetcode 17. Letter Combinations of a Phone Number
- 17.leetcode Letter Combinations of a Phone Number(meidum)[递归回溯]