leecode第十七题(话号码的字母组合)

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        int len=digits.size();
        vector<string> result;
        if (len==0)
            return result;
        else//先把第一个数字对应的字母打入result,这是因为避免core_code里第一个循环无法重复
        {
            int temp_len=digits[0]-'0';
            int temp_num_max= (temp_len==9||temp_len==7)? 4:3;
            for(int i=0;i<temp_num_max;i++)
            {
                char ch=3*(temp_len-2)+'a'+i;
                if(temp_len>7)//眼瞎了一下,没看到7也是四个字母
                    ch++;
                string temp;
                temp.push_back(ch);
                result.push_back(temp);
            }
        }
        
        core_code(result,digits.substr(1));//str.substr的用法
        
        return result;
    }
    
    void core_code(vector<string>& result,string digits) {
        int len=digits.size();
        if (len==0)
            return;
        
        int temp_len=digits[0]-'0';
        int temp_num_max= (temp_len==9||temp_len==7)? 4:3;
        bool flag=true;
        int result_len=result.size();
        
        for(int i=1;i<temp_num_max;i++)//先对result内容重复temp_num_max遍
        {
            for(int j=0;j<result_len;j++)
                result.push_back(result[j]);
        }
        
        for(int i=0;i<temp_num_max;i++)//再对每个打入相应的字母
        {
            char ch=3*(temp_len-2)+'a'+i;
            if(temp_len>7)
                ch++;
            for(int j=0;j<result_len;j++)
                result[i*result_len+j].push_back(ch);
        }
        
        core_code(result,digits.substr(1));
    }
};

分析:

我好久好久没刷题了,最近写论文太恶心了。好在这个题不难,就是写的又臭又长。

我要回归了!

 

转载于:https://www.cnblogs.com/CJT-blog/p/11160187.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值