leetcode Letter Combinations of a Phone Number

7675 ワード

このような問題をまとめたのも、昨日leetcode Generate Parenthesesと書き間違えた反省だった.これも配列を再帰的に記入する問題で、ここでは再帰的に1つ記入することを意識していますが、この問題の影響で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 }