Leetcode 17. 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.

分析

手机上的拼音输入法,每个数字键代表多个字母。
根据输入的数字串,返回可能的字符串
下面的C代码已通过。
先初始化二维字符数组,然后如果出现0或者1,或者数字串为空,就直接返回空。然后再依次递归判断接下来的字符,根据不同的数字表示不同的字符,之后在找到最后一个字符的时候,将该字符串存入结果字符串数组中。

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */

void combination(char *digits,int digitsLength,char ** ans,int *returnSize,char *temp,int tempLength)
{
    if(digits[digitsLength]==NULL)
    {
        char * ansTemp=(char *)malloc(sizeof(char)*1000);
        int i=0;
        for(i=0;i<tempLength;i++)
        ansTemp[i]=temp[i];
        ansTemp[i]='\0';
        ans[*returnSize]=ansTemp;
        *returnSize=*returnSize+1;
        //printf("%s %d\n",ansTemp,*returnSize);
    }
    else if(digits[digitsLength]=='2')
    {
        digitsLength++;
        temp[tempLength]='a';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='b';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='c';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='3')
    {
        digitsLength++;
        temp[tempLength]='d';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='e';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='f';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='4')
    {
        digitsLength++;
        temp[tempLength]='g';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='h';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='i';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='5')
    {
        digitsLength++;
        temp[tempLength]='j';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='k';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='l';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='6')
    {
        digitsLength++;
        temp[tempLength]='m';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='n';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='o';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='7')
    {
        digitsLength++;
        temp[tempLength]='p';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='q';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='r';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='s';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='8')
    {
        digitsLength++;
        temp[tempLength]='t';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='u';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='v';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
    else if(digits[digitsLength]=='9')
    {
        digitsLength++;
        temp[tempLength]='w';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='x';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='y';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        temp[tempLength]='z';
        combination(digits,digitsLength,ans,returnSize,temp,tempLength+1);
        tempLength++;
    }
}
char** letterCombinations(char* digits, int* returnSize) {
    int i=0;
    char **ans=(char **)malloc(sizeof(char *)*1000000);
    *returnSize=0;
    char *temp=(char *)malloc(sizeof(char)*1000);
    if(digits[i]=='\0')
    return NULL;
    while(digits[i]!='\0')
    {
        if(digits[i]=='0'||digits[i]=='1')
            return NULL;
        i++;
    }
    combination(digits,0,ans,returnSize,temp,0);
    return ans;
}

当然还有更简单的方法,先处理前两个数字尽可能多的字符串,之后依次根据后面的数字再组成更多的字符串,可能需要重复赋值,但是相对的内存空间消耗较少。而我的方法多次递归,消耗大量栈空间。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容