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"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
class Solution {
public:
vector<string> res;
string path;
string strT[10] = {"","","abc","def","ghi","jkl","mno","qprs","tuv","wxyz"};
vector<string> letterCombinations(string digits) {
dfs(digits,0);
return res;
}
void dfs(string s,int dep){
if(dep == s.size()){
res.push_back(path);
return;
}
int num = s[dep] - '0';
string tmp = strT[num];
for(int j = 0;j < tmp.size();j ++){
path.push_back(tmp[j]);
dfs(s,dep + 1);
path.pop_back();
}
}
};
通过给定的数字字符串,实现将其转换为对应的字母组合,并详细解释了实现过程和使用的方法。
559

被折叠的 条评论
为什么被折叠?



