博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Letter Combinations of a Phone Number
阅读量:6296 次
发布时间:2019-06-22

本文共 4536 字,大约阅读时间需要 15 分钟。

题目:Letter Combinations of a Phone Number

将电话号码的组合转换称所有可能的字母组合。

思路:

首先将电话键盘中的0->9的数字对应的字母集合放到一个常量二维数组中便于搜索。

[

  [' ']//0

  []//1

  ['a','b','c']//2

  ...

  ['w','x','y','z']//9

]

然后递归搜索每一位给定的数字对应的字母数组,一一组合。

得到返回后将其与当前数字的字母数组组合得到新的数组在返回。

1 /***************************************************************************************************  2 Given a digit string, return all possible letter combinations that the number could represent.  3 A mapping of digit to letters (just like on the telephone buttons) is given below.  4 1(0_0)    2(abc)    3(def)  5 4(ghi)    5(jkl)    6(mno)  6 7(pqrs)    8(tuv)    9(wxyz)  7                 0( )  8 Input:Digit string "23"  9 Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 10 Note: 11 Although the above answer is in lexicographical order, your answer could be in any order you want. 12 ***************************************************************************************************/ 13 #include
14 #include
15 16 char **numToString(char **letters,int *retSize,int *lengths,char *digits,int pos){ 17 int i,num = digits[pos] - '0'; 18 if(pos == 0){
//递归是反着的,当到第一个数字时开始返回 19 char **s = (char **)malloc(lengths[num]*sizeof(char *)); 20 for(i = 0;i < lengths[num];i++){ 21 s[i] = (char *)malloc(2*sizeof(char)); 22 s[i][0] = letters[num][i]; 23 s[i][1] = '\0';//注意数组单个赋值末尾需手动加'\0' 24 } 25 *retSize = lengths[num]; 26 return s; 27 } 28 int k = 0,subLen; 29 char **subs = numToString(letters,&subLen,lengths,digits,pos - 1); 30 char **rets = (char **)malloc(subLen*lengths[num]*sizeof(char *)); 31 for(i = 0;i < subLen;i++){ 32 for(int j = 0;j < lengths[num];j++){
//在末尾加上当前数字对应的字母 33 rets[k] = (char *)malloc((pos + 2)*sizeof(char)); 34 //memset(rets[k],0,(pos + 2)*sizeof(char)); 35 strncpy(rets[k],subs[i],pos);//复制前面的转换结果,一个要复制当前数字对应的字母的数量的次数 36 rets[k][pos] = letters[num][j]; 37 rets[k][pos + 1] = '\0'; 38 k++; 39 } 40 free(subs[i]); 41 subs[i] = NULL; 42 } 43 free(subs); 44 45 *retSize = k; 46 return rets; 47 } 48 49 /** 50 * Return an array of size *returnSize. 51 * Note: The returned array must be malloced, assume caller calls free(). 52 */ 53 char** letterCombinations(char* digits, int* returnSize) { 54 if(digits == NULL || strlen(digits) < 1){ 55 *returnSize = 0; 56 return NULL; 57 } 58 int i; 59 int lengths[] = {
1,0,3,3,3,3,3,4,3,4}; 60 char **letters = (char **)malloc(10*sizeof(char *)); 61 letters[0] = (char*)malloc(sizeof(char)); 62 letters[0][0] = ' '; 63 letters[1] = NULL; 64 letters[2] = (char*)malloc(4*sizeof(char)); 65 letters[2][0] = 'a';letters[2][1] = 'b';letters[2][2] = 'c';letters[2][3] = '\0'; 66 letters[3] = (char*)malloc(4*sizeof(char)); 67 letters[3][0] = 'd';letters[3][1] = 'e';letters[3][2] = 'f';letters[3][3] = '\0'; 68 letters[4] = (char*)malloc(4*sizeof(char)); 69 letters[4][0] = 'g';letters[4][1] = 'h';letters[4][2] = 'i';letters[4][3] = '\0'; 70 letters[5] = (char*)malloc(4*sizeof(char)); 71 letters[5][0] = 'j';letters[5][1] = 'k';letters[5][2] = 'l';letters[5][3] = '\0'; 72 letters[6] = (char*)malloc(4*sizeof(char)); 73 letters[6][0] = 'm';letters[6][1] = 'n';letters[6][2] = 'o';letters[6][3] = '\0'; 74 letters[7] = (char*)malloc(5*sizeof(char)); 75 letters[7][0] = 'p';letters[7][1] = 'q';letters[7][2] = 'r';letters[7][3] = 's';letters[7][4] = '\0'; 76 letters[8] = (char*)malloc(4*sizeof(char)); 77 letters[8][0] = 't';letters[8][1] = 'u';letters[8][2] = 'v';letters[8][3] = '\0'; 78 letters[9] = (char*)malloc(5*sizeof(char)); 79 letters[9][0] = 'w';letters[9][1] = 'x';letters[9][2] = 'y';letters[9][3] = 'z';letters[9][4] = '\0'; 80 81 char **ss = numToString(letters,returnSize,lengths,digits,strlen(digits) - 1); 82 83 for(int i = 0;i < 10;i++){ 84 free(letters[i]); 85 letters[i] = NULL; 86 } 87 free(letters); 88 89 return ss; 90 } 91 92 void main(){ 93 char digits[] = "32";//会自动加'\0' 94 int size = 0; 95 char **retstr = letterCombinations(digits,&size); 96 for(int i = 0;i < size;i++){ 97 printf("%s\n",retstr[i]); 98 free(retstr[i]); 99 }100 free(retstr);101 }

 

转载于:https://www.cnblogs.com/yeqluofwupheng/p/6666823.html

你可能感兴趣的文章
(转)从给定的文本中,查找其中最长的重复子字符串的问题
查看>>
HDU 2159
查看>>
spring batch中用到的表
查看>>
资源文件夹res/raw和assets的使用
查看>>
UINode扩展
查看>>
LINUX常用命令
查看>>
百度云盘demo
查看>>
概率论与数理统计习题
查看>>
初学structs2,简单配置
查看>>
Laravel5.0学习--01 入门
查看>>
时间戳解读
查看>>
sbin/hadoop-daemon.sh: line 165: /tmp/hadoop-hxsyl-journalnode.pid: Permission denied
查看>>
@RequestMapping 用法详解之地址映射
查看>>
254页PPT!这是一份写给NLP研究者的编程指南
查看>>
《Data Warehouse in Action》
查看>>
String 源码浅析(一)
查看>>
Spring Boot 最佳实践(三)模板引擎FreeMarker集成
查看>>
Fescar 发布 0.2.3 版本,支持 Redis 和 Apollo
查看>>
Google MapReduce到底解决什么问题?
查看>>
CCNP-6 OSPF试验2(BSCI)
查看>>