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

        题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。

    注意,当n=5时,输出的是11-12-21,而不是1231,并不是简单的计数,而是要查询连续的1,连续的2。。。的个数。

    第一版 只查询0~9 的个数的版本,错误!

    另外,注意,对于 for(size_t i =10; i>=0; i++)遍历时,会越界,因为i是无符号数,用于不可能为负数,所以这里要用int或者 signed size_t(ssize_t)

    size_t与ssize_t
    ssize_t是什么类型的?
    解释一:为了增强程序的可移植性,便有了size_t,它是为了方便系统之间的移植而定义的,不同的系统上,定义size_t可能不一样。
         在32位系统上 定义为 unsigned int 也就是说在32位系统上是32位无符号整形。在64位系统上定义为 unsigned long 也就是说在64位系统上是64位无符号整形。size_t一般用来表示一种计数,比如有多少东西被拷贝等。例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。 它的意义大致是“适于计量内存中可容纳的数据项目个数的无符号整数类型”。所以,它在数组下标和内存管理函数之类的地方广泛使用。而ssize_t这个数据类型用来表示可以被执行读写操作的数据块的大小.它和size_t类似,但必需是signed.意即:它表示的是signed size_t类型的。

    typedef unsigned long size_t
    解释二:ssize_t是signed size_t,
    size_t是标准C库中定义的,应为unsigned int。定义为typedef int ssize_t。

    而ssize_t:这个数据类型用来表示可以被执行读写操作的数据块的大小.它和size_t类似,但必需是signed.意即:它表示的是sign size_t类型的。

    class Solution {
        public:
            string count(const string& str)
            {   
                vector<int> counts;
                counts.resize(10);
                string result;
                for(size_t i = 0; i < str.size(); i++)
                {   
                    counts[str[i] - '0'] ++; 
                }   
    
                for(int i = 9; i >=0; i--)
                {   
                    //cout << i <<"	-->"  << counts[i] << endl;
                    if(counts[i] > 0)
                    {   
                        result += (counts[i] + '0');
                        result += ( i + '0');
                    }   
                }   
                return result;
            }   
            string countAndSay(int n)  
            {   
               if(n == 1)
                return "1";
        
                string str = "1";
        
                for(int i = 1; i < n; i++)
                {   
                    str = count(str);
                }   
                return str;
            }
    };

    第二个版本,查询连续数字的个数,AC了,对于最后一个字符串的处理,其实可以在原有的字符串基础上增加一个空格,来处理,

    class Solution {
        public:
            string count(const string& str)
            {
                int counter = 0;
                string result;
                char pre = str[0];
    
                for(size_t i = 0; i < str.size(); i++)
                {
                    if(str[i] == pre)
                    {
                        counter ++;
                    }
                    else
                    {
                        result += ( counter + '0');
                        result += pre ;
                        pre = str[i];
                        counter = 1;
                    }
    
                    //handle the last char
                    if(i == str.size() -1)
                    {
                        if(str[i] == pre)
                        {
                            result += ( counter + '0');
                            result += pre ;
                        }
                        else
                        {
                            result += '1';
                            result += str[i];
                        }
                    }
    
                }
    
                return result;
            }
            string countAndSay(int n)
            {
               if(n == 1)
                return "1";
    
                string str = "1";
    
                for(int i = 1; i < n; i++)
                {
                    str = count(str);
                }
                return str;
            }
    };

     加上空格的版本:

    class Solution {
        public:
            string count(string str)
            {
                int counter = 0;
                string result;
                char pre = str[0];
                str += ' ';
    
                for(size_t i = 0; i < str.size(); i++)
                {
                    if(str[i] == pre)
                    {
                        counter ++;
                    }
                    else
                    {
                        result += ( counter + '0');
                        result += pre ;
                        pre = str[i];
                        counter = 1;
                    }
    #if 0
                    //handle the last char
                    if(i == str.size() -1)
                    {
                        if(str[i] == pre)
                        {
                            result += ( counter + '0');
                            result += pre ;
                        }
                        else
                        {
                            result += '1';
                            result += str[i];
                        }
                    }
    #endif
                }
    
                return result;
            }
            string countAndSay(int n)
            {
               if(n == 1)
                return "1";
    
                string str = "1";
    
                for(int i = 1; i < n; i++)
                {
                    str = count(str);
                }
                return str;
            }
    };
  • 相关阅读:
    mysql--主从复制
    Linux--部署Vue项目
    Linux--安装node.js
    Linux--防火墙和vim
    go.js
    服务端和客户端通信的三种方式
    mysql-排序过程
    数据分析-Numpy
    shell
    Linux-crontab
  • 原文地址:https://www.cnblogs.com/diegodu/p/4283953.html
Copyright © 2011-2022 走看看