zoukankan      html  css  js  c++  java
  • 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));
        }
    };

    分析:

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

    我要回归了!

  • 相关阅读:
    Erlang/OTP:基于Behaviour的回调函数
    使用ACE创建进程
    linux查看硬件信息
    测试~~
    很好的:纠错函数linux
    转帖
    sss
    转帖
    普通函数、虚函数、纯虚函数、
    ACE_Event_Handle
  • 原文地址:https://www.cnblogs.com/CJT-blog/p/11160187.html
Copyright © 2011-2022 走看看