码迷,mamicode.com
首页 > 其他好文 > 详细

17. Letter Combinations of a Phone Number

时间:2017-10-13 12:27:43      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:finish   abc   stdio.h   lock   ==   bin   es2017   调用   ati   

 

其实这个题目和(1,2,3,4,......)这种数的全排列组合是类似的

 

技术分享

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 char** letterCombinations(char* digits, int* returnSize);
  4 int main() {
  5 
  6     char* digits = "2345";
  7     int returnSize = 0;
  8     char ** res = letterCombinations(digits, &returnSize);
  9     int i=0;
 10     printf("===================\n");
 11     for(i=0; i<returnSize; i++) {
 12         printf("%s\n", res[i]);
 13     }
 14     return 0;
 15 }
 16 
 17 /**
 18  * Return an array of size *returnSize.
 19  * Note: The returned array must be malloced, assume caller calls free().
 20  */
 21 char** letterCombinations(char* digits, int* returnSize) {
 22     int i=0,j=0, k=0, l=0, position=0;
 23     char** res = NULL;
 24     char* buttonMap[] = {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
 25 
 26     /* get length of digits */
 27     if(digits == NULL)             /* digits is NULL */
 28         i = 0;
 29     else {
 30         while(digits[i] != ‘\0‘) /* digits is NOT NULL, but its length is 0 */
 31             i++;
 32     }
 33 
 34     if(i==0) {                     /* nothing input */
 35         res = NULL;
 36         *returnSize = 0;
 37     } else if(i == 1) {             /* recurse is to finish */
 38         position = digits[0] - 48;
 39         res = (char**) malloc(sizeof(char *) * i); /* last digit, every char as a string */
 40         while(buttonMap[position][j] != ‘\0‘) {
 41             res[j] = (char *) malloc(sizeof(char)*2);
 42             res[j][0] = buttonMap[position][j];
 43             res[j][1] = ‘\0‘;
 44             j++;
 45         }
 46         *returnSize = j;
 47     } else {                    /* recurse dose not finish yet */
 48         char ** temp = letterCombinations(digits+1, returnSize); /* recurse call */
 49 
 50         printf("===================\n");
 51         for(l=0; l<(*returnSize); l++)
 52             printf("%s\n", temp[l]);
 53 
 54         position = digits[0] - 48;
 55         int len = 0;
 56         while(buttonMap[position][len] != ‘\0‘)
 57             len++;
 58         int size = len * (*returnSize); /* m * n strings */
 59         res = (char **) malloc(sizeof(char *) * size);
 60         int lenTemp = 0;
 61         while(temp[0][lenTemp] != ‘\0‘)
 62             lenTemp++;
 63         for(j=0; j<len; j++) {
 64             for(k=0; k<(*returnSize); k++) {
 65                 res[j*(* returnSize) + k] = (char *) malloc(sizeof(char) * (lenTemp+2));
 66                 res[j * ( * returnSize) + k][0] = buttonMap[position][j];
 67                 l=0;
 68                 while(temp[k][l] != ‘\0‘) {
 69                     res[j* (*returnSize) + k][l+1] = temp[k][l];
 70                     l++;
 71                 }
 72                 res[j* (*returnSize) + k][l+1] = ‘\0‘;
 73             }
 74         }
 75         *returnSize = size;
 76     }
 77     return res;
 78 }
 79 

 

缺点是每次递归调用都要进行内存分配这个很不好。。。

 

思考一下如何用非递归的方法来做

17. Letter Combinations of a Phone Number

标签:finish   abc   stdio.h   lock   ==   bin   es2017   调用   ati   

原文地址:http://www.cnblogs.com/tuhooo/p/7660289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!