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. 问题:css 自动换行;结果:CSS控制文本自动换行

    CSS控制文本自动换行 CSS控制文本自动换行,阅读CSS控制文本自动换行,1.你定死表格的宽度,即给表格一个宽度值(是 数值,不是百分比) 2.强制不换行div{//white-space:不换行; ...

  2. 问题:C#根据生日计算属相;结果:C#实现根据年份计算生肖属相的方法

    这篇文章主要介绍了C#实现根据年份计算生肖属相的方法,涉及C#数组与字符串的操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下   本文实例讲述了C#实现根据年份计算生肖属相的方法.分享给大家供大家 ...

  3. 浅谈Android四大组建之一Service---Service与Activity的绑定

    从上一篇文章我们学会了如何创建Service,我们通过监听一个按钮,然后再按钮里面通过意图来启动Service.但是我们有没有发现,启动服务以后,Activity和Service之间的联系好像就断开了 ...

  4. MyBatis总结四:配置文件xml详解

    XML 映射配置文件 MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 ...

  5. js中FOR循环的陷阱

    //闭包解决 循环输出的问题 for(var i=0;i<rows.length;i++) {( function (i) { })(i);

  6. 【总结整理】WebGIS学习-thinkGIS(地理常识):

    ##地图知识 ###地图定义 地图是按照一定的法则,有选择地以二维或多维形式与手段在平面或球面上表示地球(或其它星球)若干现象的图形或图像,它具有严格的数学基础.符号系统.文字注记,并能用地图概括原则 ...

  7. HDU 4899 Hero meet devil (状压DP, DP预处理)

    题意:给你一个基因序列s(只有A,T,C,G四个字符,假设长度为n),问长度为m的基因序列s1中与给定的基因序列LCS是0,1......n的有多少个? 思路:最直接的方法是暴力枚举长度为m的串,然后 ...

  8. Codeforces 1142D(dp)

    题目传送 先给出设计dp的结论: dp[i][j]:以第i个位置.以rankj的数拓展出去的方案数.意会一下,我实在想不好语言-- 其中所谓rankj=真·rank%11 找到拓展的规律,转移也就顺理 ...

  9. keystone组件

    引:  什么是keystone    为何要有keystone     keystone的功能     keystone概念详解     keystone与openstack其他组件关系      k ...

  10. Inheritance with EF Code First: Part 1 – Table per Hierarchy (TPH)

    以下三篇文章是Entity Framework Code-First系列中第七回:Entity Framework Code-First(7):Inheritance Strategy 提到的三篇.这 ...