标签:pop rac solution else c++ 技术分享 flag void 直接
简单的回溯法!
class Solution { public: void backTrack(string digits, vector<string> words, string ans, vector<string>& res, int k, int flag[]) { if(k == digits.size()) { res.push_back(ans); } else { for(int i=0; i<words[(int)(digits.at(k))-(int)‘0‘].size(); i++) { string t = ans; ans.push_back(words[(int)(digits.at(k))-(int)‘0‘].at(i)); backTrack(digits,words,ans,res,k+1,flag); ans = t;//也可以直接ans.pop_back(),而不使用中间变量t } } } vector<string> letterCombinations(string digits) { string ans; int flag[4] = {0,};//0为未用过 vector<string> words = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector<string> res; if(digits == "") return res; backTrack(digits,words,ans,res,0,flag); return res; } };
17. Letter Combinations of a Phone Number C++回溯法
标签:pop rac solution else c++ 技术分享 flag void 直接
原文地址:https://www.cnblogs.com/tornado549/p/9986708.html