Letter Combinations of a Phone Number

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"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

思路:

深搜

代码:

 1     string table[10] = {"","", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
 2     void search(vector<string> &result, string tmp, string digits, int cur){
 3         if(cur == digits.length()){
 4             result.push_back(tmp);
 5             return;
 6         }
 7         tmp += " ";
 8         for(int i = 0; i < table[digits[cur]-'0'].length(); i++){
 9             tmp[cur] = table[digits[cur]-'0'][i];
10             search(result, tmp, digits, cur+1);
11         }
12     }
13     vector<string> letterCombinations(string digits) {
14         vector<string> result;
15         string tmp = "";
16         search(result, tmp, digits, 0);
17         return result;
18     }
原文地址:https://www.cnblogs.com/waruzhi/p/3416133.html