标签:
This is just a combination. Use hashtable to hold the number ==> chars
notes:
1. check corner case : input is empty, do not return vector contains empty string.
2. mapping is char to string. Do not use index to string mapping
1 class Solution { 2 private: 3 unordered_map<char, string> dict; 4 public: 5 void getComb(vector<string> &result, string s, string current, int index) { 6 if (current.size() == s.size()) { 7 result.push_back(current); 8 return; 9 } 10 for (int i = 0; i < dict[s[index]].size(); i++) { 11 getComb(result, s, current + dict[s[index]][i], index+1); 12 } 13 } 14 vector<string> letterCombinations(string digits) { 15 vector<string> result; 16 if (digits.size() == 0) return result; 17 dict[‘2‘] = "abc"; 18 dict[‘3‘] = "def"; 19 dict[‘4‘] = "ghi"; 20 dict[‘5‘] = "jkl"; 21 dict[‘6‘] = "mno"; 22 dict[‘7‘] = "pqrs"; 23 dict[‘8‘] = "tuv"; 24 dict[‘9‘] = "wxyz"; 25 getComb(result, digits, "", 0); 26 return result; 27 } 28 };
LeetCode – Refresh – Letter Combination of a Phone Number
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/4352667.html