标签:leetcode acm 算法 letter combinations
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
class Solution { public: const vector<string> keyboard{ " ", "", "abc", "def", // '0','1','2',... "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; vector<string> letterCombinations(const string &digits) { vector<string> result; dfs(digits, 0, "", result); return result; } void dfs(const string &digits, size_t cur, string path, vector<string> &result) { if (cur == digits.size()) { result.push_back(path); return; } for (auto c : keyboard[digits[cur] - '0']) { dfs(digits, cur + 1, path + c, result); } } };
leetcode.17-----------Letter Combinations of a Phone Number
标签:leetcode acm 算法 letter combinations
原文地址:http://blog.csdn.net/chenxun_2010/article/details/43458247