const auto io_sync_off = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return nullptr; } (); class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param digits string字符串 * @return string字符串vector */ vector<string> letterCombinations(string digits) { int n = digits.size(); if (!n) return {}; // write code here unordered_map<char, string>mp { {'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"} }; string buf; dfs(buf, digits, 0, mp); return ans; } vector<string> ans; void dfs(string& buf, string& s, int i, unordered_map<char, string> mp) { if (i == s.size()) { ans.push_back(buf); return ; } auto& str = mp[s[i]]; for (char f : str) { buf.push_back(f); dfs(buf, s, i + 1, mp); buf.pop_back(); } } }; #ifdef debug int main() { cout << __FILE__ << endl; Solution k; println(k.letterCombinations("34")); return 0; } #endif