题解 | #牛名生成器#
牛名生成器
https://www.nowcoder.com/practice/f82fe408de8f4fbdbc30162d6b3e65bb
#include <unordered_map> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param digits string字符串 * @return string字符串vector */ vector<string> ans; void dfs(string digits, int start, string str, unordered_map<char, string> u_m) { int len = digits.size(); if(start==len) { ans.emplace_back(str); return; } char num = digits[start]; string t_str = u_m[num]; for(int i=0; i<t_str.size(); ++i) dfs(digits, start+1, str+t_str[i], u_m); return; } vector<string> letterCombinations(string digits) { // write code here unordered_map<char, string> u_m = {{'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"}}; string str = ""; dfs(digits, 0, str, u_m); return ans; } };