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. 10-17C#语句(3)--跳转语句、异常处理语句

    回顾: 穷举法(重点掌握):虽然运用for...嵌循环语句,但是也要找到执行for...循环的规律, 即一个题目中,需要得到哪个值,首先定义它初始变量:哪个条件需要改变,它对应的就是for...循环的 ...

  2. vb和dos批处理创建或生成快捷方式

    https://www.cnblogs.com/gszhl/archive/2009/04/23/1441753.html vb和dos批处理创建或生成快捷方式   首先说我现在用的一种,最有效的也是 ...

  3. MySQL建立一个连接工具类

    public class DBUtil { public static Connection getConn() { Connection conn = null; try { Class.forNa ...

  4. struts-hibernate整合(1)配置环境

    ①加载jar包 创建类库: 在myeclipse中点击windows---Preference---Java---Build Path---User Libraries---new 输入创建类库名字s ...

  5. ssh整合思想

    整合过程:

  6. java如何从cmd运行并使用text文件作为输入源的总结

    大家好,好几天没写东西了,又和大家见面了 首先,编译我们的.java文件,生成.class文件 使用命令 javac yourname.java 编译 然后使用java youname < yo ...

  7. ROS Learning-001 安装 ROS indigo

    如何在 Ubuntu14.04 上安装 ROS indigo 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LTS ROS 版本 ...

  8. PCL—关键点检测(iss&Trajkovic)低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5069311.html 关键点检测往往需要和特征提取联合在一起,关键点检测的一个重要性质就是旋转不变性,也就是说,物 ...

  9. p2421 荒岛野人

    传送门 题目 克里特岛以野人群居而著称.岛上有排列成环行的M个山洞.这些山洞顺时针编号为1,2,…,M.岛上住着N个野人,一开始依次住在山洞C1,C2,…,CN中,以后每年,第i个野人会沿顺时针向前走 ...

  10. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...