简单DFS题目
代码:
1 vector<string> res; 2 3 void dfs(string &digits, vector<string> &i2s, string ans, int pos) { 4 if (pos == digits.length()) { 5 res.push_back(ans); 6 return; 7 } 8 9 for (auto c : i2s[digits[pos] - '0']) 10 dfs(digits, i2s, ans + c, pos + 1); 11 } 12 13 vector<string> letterCombinations(string digits) { 14 vector<string> i2s {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; 15 dfs(digits, i2s, "", 0); 16 return res; 17 }
本文详细解析了一道经典的深度优先搜索(DFS)算法题目——电话号码的字母组合。通过递归的方式,实现了从电话键盘上的数字到对应字母的所有可能组合,展示了DFS算法在解决此类问题上的应用。

125

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



