[C++]LeetCode: 20 Count and Say

题目:

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

初始思路:题目要求产生count-and-say数列的第n个数列值;按照count-and-say构造思路去一步步构造数列值

Attention: 注意考虑是最后一位的情况,仍然相等,具体看循环判断条件;curString +=count+'0';解决int转char问题。

结果:超时,这种方法太笨。效率不高。

Submission Result: Output Limit Exceeded

Error Code:

class Solution {
public:
    string countAndSay(int n) {
        //产生count-and-say数列的第n个数列值
        //思路:按照count-and-say构造思路去一步步构造数列值
        //检查当前string的值,直到出现不重复的数字,push_back新的string
        //Attention: 注意考虑是最后一位的情况,仍然相等,具体看循环判断条件。
        
        string ret, tmp;
        int cnt = 0;        //表示重复数字的个数
        int j = 0;          //标记本次重复数字的起点
        char cntchar, num;
        
        ret.push_back('1');
        
        //构造n-1次
        for(; n > 1; n--)
        {
            //每次内部读取构造过程
            num = ret[0];
            for(int i = 0; i < ret.length(); i++)
            {
                if(ret[i] != num)
                {
                    cnt = i - j; 
                    //cntchar = stdlib::itoa(cnt);
                    cntchar = cnt + '0';
                    tmp.push_back(cnt);
                    tmp.push_back(num);
                    j = i;
                    num = ret[i];
                }
                else if(ret[i] == num && i == ret.length() - 1)
                {
                    cnt = i - j + 1; 
                    cntchar = cnt + '0';
                    tmp.push_back(cnt);
                    tmp.push_back(num);
                }
            }
            
            ret = tmp;
        }
        
        return ret;
    }
};


思路:每次记录重复的数字和次数,下次计数时重置。

Attention: 使用string的迭代器的方法;int 转char的方法;循环初始是从int =1开始,进行n-1次循环。

复杂度:O(n)

AC Code:

class Solution {
public:
    string countAndSay(int n) {
        string ret, t;
        ret.push_back('1');
        
        for(int i = 1; i < n; i++)
        {
            //此处string迭代器的使用要记得,可以使用迭代器操作string(类似指针操作)
            for(string::const_iterator p = ret.begin(), pe = ret.end(); p != pe ;)
            {
                //记录下每次的cnt和c
                int cnt = 1;
                char c = *p++;
                while(p != pe && *p == c)
                {
                    ++cnt;
                    ++p;
                }
                //int转char的方法
                t += cnt + '0';
                t += c;
            }
            
            //此处使用赋值和swap都可以。
            ret.swap(t);
            //ret = t;
            t.clear();
        }
    
        return ret;
    }
    
};

不使用迭代器版本的AC Code:

class Solution {
public:
    string countAndSay(int n) {
        string prevString;
        string curString = "1";
        for (int i = 1; i<n; ++i){
            prevString = curString;
            curString = "";
            int count = 1;
            char digit = prevString[0];
            for (int j = 1; j<prevString.length(); ++j){
                if (prevString[j] == digit){
                    ++count;
                }
                else{

                    curString +=count+'0'; //myItoa(count);
                    curString += digit;
                    digit = prevString[j];
                    count = 1;
                }
            }
            curString += count+'0';//myItoa(count);
            curString += digit;
        }
        return curString;
    }
/*private:
    string myItoa(int i){
        string str;
        while (i){
            str += i%10+'0';
            i /=10;
        }
        reverse(str.begin(), str.end());
        return str;
    }*/
};



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值