leetcode Letter Combinations of a Phone Number

对这种问题进行一下小结也算是对昨天写错 leetcode Generate Parentheses 的一个反省。这也是一个用递归去填数组的问题,在这里就意识到了一次递归填一个,可能是受到这个题的影响所以才会出现在leetcode Generate Parentheses 里的问题,果然和大神们还是有不小差距的。

代码:

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 vector<string> cs = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
 8 vector<string> *result = new vector<string>();
 9 
10 vector<string> F(string digits, string s)
11 {
12     int size = digits.length();
13     if (size < 1)
14         return *result;
15     if (size == 1)
16     {
17         int L = cs[digits[0] - '0'].length();
18         for (int i = 0; i < L; i++)
19         {
20             string t = s;
21             t += cs[digits[0] - '0'][i];
22             result->push_back(t);
23         }
24     }
25     else
26     {
27         int L = cs[digits[0] - '0'].length();
28         for (int i = 0; i < L; i++)
29         {
30             string t = s;
31             t += cs[digits[0] - '0'][i];
32             F(digits.substr(1), t);
33         }
34     }
35     return *result;
36 }
37 
38 vector<string> letterCombinations(string digits) {
39     return F(digits, "");
40 }
41 
42 
43 int main()
44 {
45     letterCombinations("23");
46 }
原文地址:https://www.cnblogs.com/chaiwentao/p/4427355.html