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"].
水题: DFS
int counts[] = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4}; char letter[] = {'0', '0', 'a', 'd', 'g', 'j', 'm', 'p', 't', 'w'}; class Solution { public: void DFS( string & digit,int i, string s) { if(i == size){ result.push_back(s); return ; } int index = digit[i] - '0' ; for(int j = 0; j < counts[index] ; j++) { char c = letter[index]+j ; DFS(digit, i+1, s+c); } } vector<string> letterCombinations(string digits) { // Start typing your C/C++ solution below // DO NOT write int main() function result.clear(); size = digits.size(); string s = ""; DFS(digits, 0 , s); return result ; } private : vector<string> result ; int size ; };