您的位置:首页 > 其它

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.



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]);
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: