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 = 0:  1

n = 1:  11  (前一个是1个1)

n = 2:  21  (前一个是2个1)

n = 3:  1211 (前一个是1个2,1个1)

n = 4:  111221  (前一个是1个1,1个2,2个1)

....

所以是一个递推关系,按规则模拟即可。

 class Solution {
public:
string countAndSay(int n) {
string result("");
for (int i = ; i < n; ++i) {
stringstream ss;
int j = , N = result.size();
while (j < N) {
int c = ;
while (j + < N && result[j] == result[j + ]) {
++c;
++j;
}
ss << c << result[j];
++j;
}
ss >> result;
}
return result;
}
};

【Leetcode】Count and Say的更多相关文章

  1. 【LeetCode】Count and Say(报数)

    这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...

  2. 【leetcode】Count and Say (easy)

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

  3. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  4. 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  8. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  9. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

随机推荐

  1. jackson 进行json与java对象转换 之三

    2.测试类,没用Junit,用Main()方法输出. package test; import java.io.IOException; import java.util.ArrayList; imp ...

  2. ListView显示Sqlite的数据

    在安卓中,ListView和Sqlite都是十分常用的.这次我们来结合这个两个知识点写一个Demo. 功能:吧SQLite中的数据用ListView显示出来. 先看截图吧 首先是数据库 然后是运行截图 ...

  3. Solr5.5.3的研究之路 ---1、从Mysql导入数据并创建索引

    公司需要用到全文检索,故使用Solr,也是新人一枚,本人查看的前提是Solr已经安装部署成功,我用的服务器是自带的Jetty 1.创建Collection [root@whoami bin]# ./s ...

  4. java中的自动转型的学习理解

    java当中的继承是和c++中的继承类似,只是java中的继承时的父类只能有一位. 我们今天在这里讲的是关于java中的自动转型的理解:顾名思义,自动转型值得就是使用时自动的将自身的类型进行转化. 自 ...

  5. appium如何连接模拟器代码实例

    from appium import webdriver def connect(self): self.desired_caps = {} self.desired_caps['platformNa ...

  6. 『原』在Linux下反编译Android .apk文件 使用apktool dex2jar JD-eclipse

    一.使用apktool 将 apk反编译生成程序的源代码和图片.XML配置.语言资源等文件 具体步骤: (1)下载反编译工具包:apktool 官方的打不开 http://apktool.shouji ...

  7. MySQL存储引擎 -- MyISAM(表锁定) 与 InnoDB(行锁定) 锁定机制

    前言 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核心技术之一.本章将对MySQL中两种 ...

  8. CSS特性:white-space: nowrap;text-overflow: ellipsis;text-decoration: none

    /*white-space: nowrap; text-overflow: ellipsis; text-decoration: none;*/ /*这三句话必须连着使用, * clip: 当对象内文 ...

  9. [转]怎么学习前端,尤其是 JavaScript 这块

    1. 先看看 w3school ,了解什么是 js,再找几本写 js 小效果的书看看,知道 js 干什么: 2. 然后再去通读 API,推荐 <Javascript权威指南>,第四版吧,第 ...

  10. SpringMVC路径问题回顾,加斜杠和不加斜杠的问题(六)

    绝对路径:全的路径. 相对路径:有参照的路径. 加斜杠和不加斜杠的问题如下:(分前台和后台路径,明白这两个就知道什么意思了) 如果是页面,这个图片路径出现在jsp页面,所以是前台路径,前台路径的参照物 ...