标签:字符串 结果 cto void class int turn ret return
题目如下:
采用递归的方式,判断是否到达字符串尾部,到达尾部则添加结果到result。
代码如下:
map<char, string> cs{ { ‘2‘, "abc" }, { ‘3‘, "def" }, { ‘4‘, "ghi" }, { ‘5‘, "jkl" }, { ‘6‘, "mno" }, { ‘7‘, "pqrs" }, { ‘8‘, "tuv" }, { ‘9‘, "wxyz" } }; vector<string> result; void combine(const string &s,string tmp, int index) { string current = cs[s[index]]; if (index == s.size() - 1) { for (int i = 0; i < current.size(); i++) result.push_back(tmp + current[i]); return; } for (int i = 0; i < current.size(); i++) combine(s, tmp+current[i], index + 1); } vector<string> letterCombinations(string digits) { result.clear(); int length = digits.size(); if (length < 1) return vector<string>(); combine(digits, "", 0); return result; }
标签:字符串 结果 cto void class int turn ret return
原文地址:https://www.cnblogs.com/Oscar67/p/9069148.html