题目: 给一个9键的键盘,再给一个只有2-9的字符串,输出所有可能的字符串情况
题解:dfs全排列就好。
class Solution { public: string letters[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; void dfs(vector<string>& ans, string pre, string& digits, int index) { if (index >= digits.size()){ if (pre.size() > 0)ans.push_back(pre); }else{ string letter = letters[digits[index] - '0']; for (int i = 0; i < letter.size(); ++i){ dfs(ans, pre + letter[i], digits, index + 1); } } } vector<string> letterCombinations(string digits) { vector<string> ans; dfs(ans, "", digits, 0); return ans; } };