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"].
本文是简单的词语匹配问题,先用letter存放字母键对应的字母组,然后就是两个字符的组合问题。时间:2ms
代码如下:
class Solution { public: string getString(char s){ switch (s){ case '0':return ""; break; case '2':return "abc"; break; case '3':return "def"; break; case '4':return "ghi"; break; case '5':return "jkl"; break; case '6':return "mno"; break; case '7':return "pqrs"; break; case '8':return "tuv"; break; case '9':return "wxyz"; break; default:return ""; break; } } vector<string> letterCombinations(string digits) { vector<string> letter; int n = letter.size(); string strf = getString(digits[0]); for (int i = 0; i < strf.size(); i++){ string s = ""; s += strf[i]; letter.push_back(s); } for (int i = 1; i < digits.size(); i++){ string strl = getString(digits[i]); n = letter.size(); for (int j = 0; j < n; j++){ strf = letter[j]; for (int z = 0; z < strl.size(); z++){ letter.push_back(strf + strl[z]); } } letter.erase(letter.begin(), letter.begin() + n); } return letter; } };