您的位置:首页 > 其它

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.



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;
}

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