LeetCode 17. 电话号码的字母组合(C、C++、python)

本文介绍了一种将数字字符串转换为可能的字母组合的算法,适用于电话按键等场景。通过递增的方式,算法生成所有可能的组合,适用于多种编程语言如C、C++和Python。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

C

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
char** letterCombinations(char* digits, int* returnSize) 
{
    int n=strlen(digits);
    if(n==0)
    {
        *returnSize=0;
        return NULL;
    }
    int count=1;
    for(int i=0;i<n;i++)
    {
        if(digits[i]=='7' || digits[i]=='9')
        {
            count*=4;
        }
        else
        {
            count*=3;
        }
    }
    char** res=(char**)malloc(sizeof(char*)*count);
    for(int i=0;i<count;i++)
    {
        res[i]=(char*)malloc(sizeof(char)*n);
    }
    int k=0;
    int row=1;
    for(int i=0;i<n;i++)
    {
        switch(digits[i])
        {
            case '2':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='a';
                    res[j+row][k]='b';
                    res[j+2*row][k]='c';
                }
                k++;
                row*=3;
                break;
            case '3':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='d';
                    res[j+row][k]='e';
                    res[j+2*row][k]='f';
                }
                k++;
                row*=3;
                break;
            case '4':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='g';
                    res[j+row][k]='h';
                    res[j+2*row][k]='i';
                }
                k++;
                row*=3;
                break;
            case '5':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='j';
                    res[j+row][k]='k';
                    res[j+2*row][k]='l';
                }
                k++;
                row*=3;
                break;
            case '6':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='m';
                    res[j+row][k]='n';
                    res[j+2*row][k]='o';
                }
                k++;
                row*=3;
                break;
            case '7':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                    strcpy(res[j+3*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='p';
                    res[j+row][k]='q';
                    res[j+2*row][k]='r';
                    res[j+3*row][k]='s';
                }
                k++;
                row*=4;
                break;
            case '8':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='t';
                    res[j+row][k]='u';
                    res[j+2*row][k]='v';
                }
                k++;
                row*=3;
                break;
            case '9':
                for(int j=0;j<row;j++)
                {
                    strcpy(res[j+row],res[j]);
                    strcpy(res[j+2*row],res[j]);
                    strcpy(res[j+3*row],res[j]);
                }
                for(int j=0;j<row;j++)
                {
                    res[j][k]='w';
                    res[j+row][k]='x';
                    res[j+2*row][k]='y';
                    res[j+3*row][k]='z';
                }
                k++;
                row*=4;
                break;            
        }
    }
    for(int i=0;i<count;i++)
    {
        res[i][k]='\0';
    }
    *returnSize=count;
    return res;
}

C++

class Solution {
public:
    vector<string> letterCombinations(string digits) 
    {
        int n=digits.length();
        vector<string> ans;
        if(n==0)
        {
            return ans;
        }
        vector<string> res{""};
        for(int i=0;i<n;i++)
        {
            int m=res.size();
            switch(digits[i])
            {
                case '2':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='a';
                        res[j+m]+='b';
                        res[j+m*2]+='c';
                    }
                    break;
                case '3':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='d';
                        res[j+m]+='e';
                        res[j+m*2]+='f';
                    }
                    break;
                case '4':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='g';
                        res[j+m]+='h';
                        res[j+m*2]+='i';
                    }
                    break;
                case '5':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='j';
                        res[j+m]+='k';
                        res[j+m*2]+='l';
                    }
                    break;
                case '6':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='m';
                        res[j+m]+='n';
                        res[j+m*2]+='o';
                    }
                    break;
                case '7':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='p';
                        res[j+m]+='q';
                        res[j+m*2]+='r';
                        res[j+m*3]+='s';
                    }
                    break;
                case '8':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='t';
                        res[j+m]+='u';
                        res[j+m*2]+='v';
                    }
                    break;
                case '9':
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res.push_back(res[j]);
                    }
                    for(int j=0;j<m;j++)
                    {
                        res[j]+='w';
                        res[j+m]+='x';
                        res[j+m*2]+='y';
                        res[j+m*3]+='z';
                    }  
                    break;
            }            
        }
        return res;        
    }
};

python

class Solution:
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        n=len(digits)
        res=['']
        if n==0:
            return []
        for i in range(0,n):
            m=len(res)
            if digits[i]=='2':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='a'
                    res[j+m]+='b'
                    res[j+m*2]+='c'
            if digits[i]=='3':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='d'
                    res[j+m]+='e'
                    res[j+m*2]+='f'   
            if digits[i]=='4':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='g'
                    res[j+m]+='h'
                    res[j+m*2]+='i'
            if digits[i]=='5':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='j'
                    res[j+m]+='k'
                    res[j+m*2]+='l'
            if digits[i]=='6':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='m'
                    res[j+m]+='n'
                    res[j+m*2]+='o'
            if digits[i]=='7':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='p'
                    res[j+m]+='q'
                    res[j+m*2]+='r'
                    res[j+m*3]+='s'
            if digits[i]=='8':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='t'
                    res[j+m]+='u'
                    res[j+m*2]+='v'
            if digits[i]=='9':
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res.append(res[j])
                for j in range(0,m):
                    res[j]+='w'
                    res[j+m]+='x'
                    res[j+m*2]+='y'
                    res[j+m*3]+='z'
        return res

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值