力扣-------电话号码的字母组合

在这里插入图片描述

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
char a[8][4]={{"abc "},{"def "},{"ghi "},{"jkl "},{"mno "},{"pqrs"},{"tuv "},{"wxyz"}};
char ** letterCombinations(char * digits, int* returnSize){
    *returnSize=0;
    char** ans=(char**)calloc(144,sizeof(char*));
    if(strlen(digits)==0){
        return ans;
    }
    if(strlen(digits)==1){
        for(int b=0;b<4;b++){
            if(a[digits[0]-'2'][b]!=' '){
                ans[*returnSize]=(char*)calloc(2,sizeof(char));
                ans[*returnSize][0]=a[digits[0]-'2'][b];
                *returnSize=*returnSize+1;
            }
        }
    }
    if(strlen(digits)==2){
        for(int b=0;b<4;b++){
            if(a[digits[0]-'2'][b]!=' '){
                for(int c=0;c<4;c++){
                    if(a[digits[1]-'2'][c]!=' '){
                        ans[*returnSize]=(char*)calloc(3,sizeof(char));
                        ans[*returnSize][0]=a[digits[0]-'2'][b];
                        ans[*returnSize][1]=a[digits[1]-'2'][c];
                        *returnSize=*returnSize+1;                     
                    }
                }
            }
        }
    }
    if(strlen(digits)==3){
        for(int b=0;b<4;b++){
            if(a[digits[0]-'2'][b]!=' '){
                for(int c=0;c<4;c++){
                    if(a[digits[1]-'2'][c]!=' '){
                        for(int d=0;d<4;d++){
                            if(a[digits[2]-'2'][d]!=' '){
                                ans[*returnSize]=(char*)calloc(4,sizeof(char));
                                ans[*returnSize][0]=a[digits[0]-'2'][b];
                                ans[*returnSize][1]=a[digits[1]-'2'][c];
                                ans[*returnSize][2]=a[digits[2]-'2'][d];
                                *returnSize=*returnSize+1; 
                            }
                        }                    
                    }
                }
            }
        }
    }
    if(strlen(digits)==4){
        for(int b=0;b<4;b++){
            if(a[digits[0]-'2'][b]!=' '){
                for(int c=0;c<4;c++){
                    if(a[digits[1]-'2'][c]!=' '){
                        for(int d=0;d<4;d++){
                            if(a[digits[2]-'2'][d]!=' '){
                                for(int e=0;e<4;e++){
                                    if(a[digits[3]-'2'][e]!=' '){
                                        ans[*returnSize]=(char*)calloc(5,sizeof(char));
                                        ans[*returnSize][0]=a[digits[0]-'2'][b];
                                        ans[*returnSize][1]=a[digits[1]-'2'][c];
                                        ans[*returnSize][2]=a[digits[2]-'2'][d];
                                        ans[*returnSize][3]=a[digits[3]-'2'][e];
                                        *returnSize=*returnSize+1; 
                                    }
                                }
                            }
                        }                    
                    }
                }
            }
        }
    }
    return ans;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值