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> keyboard{" ", "","abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
vector<string> letterCombinations(string digits) {
vector<string> res;
dfs(digits, 0, "", res);
return res;
}
void dfs(string digits, int cur, string path, vector<string> &res) {
if (cur == digits.size()) {
res.push_back(path);
return ;
}
for (auto c : keyboard[digits[cur]-'0']) {
dfs(digits, cur+1, path+c, res);
}
}
};
本文介绍了一个算法问题:给定一个数字字符串,返回所有可能的字母组合。文章提供了一个C++实现示例,通过深度优先搜索(DFS)遍历所有可能的组合。
566

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



