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

    注意char * 和 string的区别。string是一个和vector类,本身是有大小的。如果只

    string str;
    str[0] = 'a';
    str[1] = 'b';
    

    是无法正常改变str的。

    class Solution {
    public:
        string countAndSay(int n) {
            string re;
            vector <int> se;
            se.push_back(1);
            if(n == 1)return "1";
            
            for(int i = 0 ; i < n-1 ;i++)se = nth(se);
            
            int j = 0;
            for(int i = 0 ; se.begin() + i != se.end() ; i++)
            {
                char temp = se[i] + '0';
                re = re + temp;
                j++;
            }
            return re;
        }
        vector<int> nth(vector<int> n )
        {
            vector<int>:: iterator  it;
            vector<int> re;
            it = n.begin();
            for(;it != n.end();)
            {
                int temp = *it;
                //cout<<temp<<endl;
                int num =1;
                it++;
                while(*it == temp && it!= n.end()){it++; num++;}
                
                re.push_back(num);
                re.push_back(temp);
            }
            return re;
        }
    };
    

      

  • 相关阅读:
    捕获组
    re.S解析
    Python eval 函数妙用
    Python tips: 什么是*args和**kwargs?
    HBase 的安装与配置
    HBase 基本操作
    HBase中的备份和故障恢复方法
    Hbase写数据,存数据,读数据的详细过程
    HBase shell
    HDFS的快照原理和Hbase基于快照的表修复
  • 原文地址:https://www.cnblogs.com/pengyu2003/p/3573876.html
Copyright © 2011-2022 走看看