电话号码的数字组合:link
1.题目分析
组合方式随着数字的增多是呈现为树状发散的方式,主要就是代码实现了。
2.示例代码
class Solution {public:vector<string> letterCombinations(string digits) {map<char, string> num2characters = {{'1', ""}, {'2', "abc"},{'3', "edf"},{'4', "ghi"}, {'5', "jkl"},{'6', "mno"},{'7', "pqrs"}, {'8', "tuv"},{'9', "wxyz"}};vector<string> ret;if(digits.empty())return ret;ret.push_back("");for (char c : digits) {vector<string> tmp;string cur = num2characters[c];for (string str : ret) {for (int i = 0; i < cur.size(); ++i) {tmp.push_back(str + cur.substr(i, 1));}}ret = tmp;}return ret;}};