一天一道LeetCode系列

(一)题目

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.

(二)解题

/*
首先初始化字符串为1,对应一个1,读成11
字符串更新为11,对应两个1,读成21
字符串更新为21,对应1个2一个1,读成1211
....
依次进行n次则得到最后的字符串
*/
class Solution {
public:
    string countAndSay(int n) {
        string str = "1";
        while(--n)
        {
            string ret;
            for (int i = 0; i < str.length(); ) {
                int count = 0;
                while (i < str.length() && str[i] == str[i + 1]) {
                    count++;
                    i++;
                }
                ret += ('1' + count);
                ret += str[i];
                i++;
            }
            str = ret;
        }
        return str;
    }
};

【一天一道LeetCode】#38. Count and Say的更多相关文章

  1. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

  2. LeetCode 38 Count and Say(字符串规律输出)

    题目链接:https://leetcode.com/problems/count-and-say/?tab=Description   1—>11—>21—>1211—>111 ...

  3. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  4. Java [leetcode 38]Count and Say

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

  5. [leetcode]38. Count and Say数数

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  6. [LeetCode] 38. Count and Say_Easy

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  7. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...

  8. leetcode 38 Count and Say ---java

    这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个 ...

  9. LeetCode - 38. Count and Say(36ms)

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

随机推荐

  1. vue 2.0 scopedSlots和slots在render函数中的应用示例

    渲染内容为: hello from functional render scopedSlots render scopedSlots named slot of render hello from f ...

  2. nginx 日志分析工具goaccess

    参考:https://www.goaccess.io/download 安装 $ wget http://tar.goaccess.io/goaccess-1.1.1.tar.gz $ tar -xz ...

  3. linux TCP头部的构造的简单分析

    TCP的头部的构造是在函数tcp_transmit_skb()中进行的 函数片段如下: /* Build TCP header and checksum it. */ th = tcp_hdr(skb ...

  4. Objective-C的继承与组合

    Objective-C的继承与组合 Objective-C与Java继承上的区别 区别 Objective-C Java 成员变量 Objective-C继承不允许子类和父类拥有相同名称的成员变量 J ...

  5. J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用

    J2EE进阶(十六)Hibernate 中getHibernateTemplate()方法使用   spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修 ...

  6. ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse

    ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse 书中,大部分出现hydro的地方,直接替换为indigo或ja ...

  7. Dynamics CRM2016 时间字段属性中的新增行为

    之前的博客中有特地介绍过CRM中的时间字段以及它在不同的应用场景中涉及的时制转换,而CRM2016又给时间字段添加了新的行为,具体见下属截图,简单介绍下每个图中对应的行为的意思,最后会做demo来具体 ...

  8. 在电脑上安装Linux操作系统

    1硬件需求 A 一台电脑 B 一个优盘 2软件需求 A制作优盘启动盘的软件PowerISO BLinux操作系统的镜像文件 3安装PowerISO,并使用PowerISO A安装PowerISO B插 ...

  9. ssh远程登录操作 和ssh信任

    ssh 可以参考上一篇telnet的文章 1.安装openssh-server     sudo dpkg -i openssh-client_1%3a5.5p1-4ubuntu6_i386.deb ...

  10. 程序员的自我修养-----Java开发的必须知道的几个注意点

    1. 将一些需要变动的配置写在属性文件中 比如,没有把一些需要并发执行时使用的线程数设置成可在属性文件中配置.那么你的程序无论在DEV环境中,还是TEST环境中,都可以顺畅无阻地运行,但是一旦部署在P ...