leetcode 17 Letter Combinations of a Phone Number

来源:互联网 发布:剑灵男力士捏脸数据 编辑:程序博客网 时间:2024/06/05 03:08

Letter Combinations of a Phone Number
Total Accepted: 54389 Total Submissions: 210023 Difficulty: Medium

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.

这道题我用了比较直接的递归方法来做。

char table[10][5]= {    {'\0','\0','\0','\0','\0'},  //0    {'\0','\0','\0','\0','\0'},  //1    {'a','b','c','\0','\0'},  //2    {'d','e','f','\0','\0'},  //3    {'g','h','i','\0','\0'},  //4    {'j','k','l','\0','\0'},  //5    {'m','n','o','\0','\0'},  //6    {'p','q','r','s','\0'},  //7    {'t','u','v','\0','\0'},  //8    {'w','x','y','z','\0'},  //9};

首先建立一个表,存上每个数字对应的字符。

char** letterCombinations(char* digits, int* returnSize) {    int length = strlen(digits);    if(length == 0)        return NULL;    for(int i = 0; i < length; i++)    {        if(digits[i] == '1' || digits[i] == '0')            return NULL;    }    int size = pow(4, length);    //cout<<size<<endl;    char **result = (char**)malloc(sizeof(char*) * size);    int count = 0;    *returnSize = combination(1, length, result, count, digits);    return result;}

对于输入有1和0的情况由于无法翻译成对应的字符,所以返回NULL。其他情况,由于每个数字最多对应4个字符,所以最多可能的字符串数量是4length。因此定义一个大小为4length的指针数组,每个指针指向一个字符串。

然后函数combination这个函数就是递归的函数了第一个参数表示当前处理的是输入的第几个数,第二个参数是输入的一串数字总共有几个,第三个参数是指向保存的字符串的指针,第四个参数是输入的这一串数字。

int combination(int d_p, int length, char** result, int count, char* digits){    if(d_p > length)    {        result[count] = (char*)malloc(sizeof(char) * (length+1));        result[count][length] = '\0';        return count + 1;    }    int index = 0;    int count2;    int count1 = count;    do    {        if(table[digits[d_p-1] - 48][index] == '\0')            break;        count2 = combination(d_p + 1, length, result, count1, digits);        for(int i = count1; i < count2; i++)        {            result[i][d_p - 1] = table[digits[d_p-1] - 48][index];        }        count1 = count2;        index++;    }while(1);    return count2;}

具体用个简单例子来说明这个过程输入的这一串数字是“2 3”,首先进入函数后判断当前检查的位数是不是大于长度,我们检查的是第1位,长度为2,所以不满足条件,然后count1表示当前在进行本次递归调用前我们已经找到了多少个可能的字符串,而count2表示进行本次递归调用后,我们找到了多少个可能的字符串。在do循环里面首先检测是否已经遍历来了当前字符的所有可能取值,因为根据表中的定义,每个字母最后一个值都是’\0’,如果取到’\0’,就表示已经取过了所有可能取值。我们递归进入下一个combination,此时检测第2位,然后在递归进入下一个combination,测试检测第3位,我们发现检测的位数超过了长度,所以我们知道我们已经到了末端了,所以为这个字符串分配内存,然后再递归返回的过程中往里面填入值。现在返回的到上一层(即第二层), 此时count2变成了1,所以在for循环中,我们可以向字符串第2位填入一个d,此时这个字符串的第1位认为空,所以这个1号字符串可以写作-d, 然后count1=count2,index++,接着再进入递归(即第三层),再为2号字符串分配空间,然后返回第二层,由于之前index已经加1,所以写入2号字符串第2位的是e,这个2号字符串可以写作-e, 此时count2为2,index++,重复这个过程,我们可以得到3号字符串为-f, count2为3, index++,此时index对应的字符为’\0’,所以返回上一层(即第一层),此时第一层的count1仍然为0,但count2已经变成了3,所以for循环将会向前3个字符串的第1位写入a,所以此时第1号字符串为ad,第2号字符串为ae,第3号字符串为af,然后count1=count2,index++,所以count1变为3,然后重新进入第二层,等到咱返回第一层时,就和之前情况相同,此时我们有了第4号字符串-d,第5号字符串-e和第6号字符串-f,此时count2为6,count1为3,所以for循环会向第4到6号字符串的第1位写入b,所以此时第4号字符串为bd,第5号字符串为be,第6号字符串为bf,然后count1=count2,index++,count1变成6,接着重新进入第二层,与之前同样的过程,我们最后会得到第7号字符串为cd,第8号字符串为ce,第9号字符串为cf,然后count1=count2,index++,count1变成9,此时index对应的字符为’\0’,说明我们已经穷尽了第一个数字对应的所有字母的情况,此时递归退出。我们需要的结果就已经存在result里了,ad, ae, af, bd, be, bf, cd, ce, cf.

下面是完整代码:

#include <iostream>#include <stdlib.h>#include <string.h>using namespace std;char table[10][5]= {    {'\0','\0','\0','\0','\0'},  //0    {'\0','\0','\0','\0','\0'},  //1    {'a','b','c','\0','\0'},  //2    {'d','e','f','\0','\0'},  //3    {'g','h','i','\0','\0'},  //4    {'j','k','l','\0','\0'},  //5    {'m','n','o','\0','\0'},  //6    {'p','q','r','s','\0'},  //7    {'t','u','v','\0','\0'},  //8    {'w','x','y','z','\0'},  //9};int combination(int d_p, int length, char** result, int count, char* digits){    if(d_p > length)    {        result[count] = (char*)malloc(sizeof(char) * (length+1));        result[count][length] = '\0';        return count + 1;    }    int index = 0;    int count2;    int count1 = count;    do    {        if(table[digits[d_p-1] - 48][index] == '\0')            break;        count2 = combination(d_p + 1, length, result, count1, digits);        for(int i = count1; i < count2; i++)        {            result[i][d_p - 1] = table[digits[d_p-1] - 48][index];        }        count1 = count2;        index++;    }while(1);    return count2;}char** letterCombinations(char* digits, int* returnSize) {    int length = strlen(digits);    if(length == 0)        return NULL;    for(int i = 0; i < length; i++)    {        if(digits[i] == '1' || digits[i] == '0')            return NULL;    }    int size = pow(4, length);    //cout<<size<<endl;    char **result = (char**)malloc(sizeof(char*) * size);    int count = 0;    *returnSize = combination(1, length, result, count, digits);    return result;}int main(){    int size, returnSize;    char *digits;    cout<<"Input the size of digits"<<endl;    cin>>size;    digits = (char*)malloc(sizeof(char) * (size+1));    for(int i = 0; i < size; i++)    {        cin>>digits[i];    }    digits[size] = '\0';    char** cl = letterCombinations(digits, &returnSize);    for(int i = 0; i < returnSize; i++)    {        printf("%s ", cl[i]);    }    return 0;}
0 0
原创粉丝点击