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.

Solution:下一个字符串= 当前字符串:∑(字符个数+字符),例如1211,下一个字符串=count(1)+'1'+count(2)+'2'+count(1)+'1'=111221

  1. class Solution {
  2. public:
  3. string countAndSay(int n) {
  4. string ret="";
  5. if(n==)return ret;
  6. while(--n){
  7. string next;
  8. char last=ret[];
  9. int i=,count=;
  10. while(i<ret.size())
  11. {
  12. while(ret[i]==last&&i<ret.size()){
  13. i++;
  14. count++;
  15. }
  16. if(i<ret.size()&&ret[i]!=last){
  17. next+=to_string(count);
  18. next+=last;
  19.  
  20. count=;
  21. last=ret[i];
  22. i++;
  23. }
  24. }
  25. next+=to_string(count);
  26. next+=last;
  27. ret=next;
  28. }
  29. return ret;
  30. }
  31. };

【LeetCode】38 - Count and Say的更多相关文章

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

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

  2. 【一天一道LeetCode】#38. Count and Say

    一天一道LeetCode系列 (一)题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, ...

  3. 【LeetCode】38. 外观数列 Count and Say

    作者: 负雪明烛 id: fuxuemingzhu 公众号:负雪明烛 本文关键词:LeetCode,力扣,算法,算法题,外观数列,Count and Say,刷题群 目录 题目描述 题目大意 解题方法 ...

  4. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

  5. 【LeetCode】730. Count Different Palindromic Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化搜索 动态规划 日期 题目地址:https:/ ...

  6. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

  7. 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  9. 【Leetcode】357. Count Numbers with Unique Digits

    题目描述: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. ...

随机推荐

  1. openfire插件开发入门1

    .案例插件的功能 这个插件很简单,就是在openfire Server启动时,和关闭时,在控制台打印出消息. 3.插件开发的目录结构设计 先来看一下当前openfire在eclipse中的目录结构: ...

  2. 甲骨文推出MySQL Fabric,简化MySQL的高可用性与可扩展性

    北京,2014年5月28日——为了满足当下对Web及云应用需求,甲骨文宣布推出MySQL Fabric.MySQL Fabric是一款可简化管理MySQL数据库群的整合式系统.该产品通过故障检测和故障 ...

  3. UVa 537 Artificial Intelligence?

    题目大意:输入一个字符串,根据物理公式P=U*I,已知其中两个量,求第三个量,结果保留两位小数.   Artificial Intelligence?  Physics teachers in hig ...

  4. WebForm页面生命周期及asp.net运行机制

    1.先上几张原理图着重理解: 现在针对第四副图原理进行解析: 流程: 1.浏览器发送请求 2.服务器软件(IIS)接收,它最终的目的就是为了向客户输出它请求的动态页面生成的html代码. 3.服务器不 ...

  5. mysql日期函数(转)

    MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...

  6. 如何拷贝CMD命令行文本到粘贴板

    /********************************************************************* * 如何拷贝CMD命令行文本到粘贴板 * To copy ...

  7. (六)6.13 Neurons Networks Implements of stack autoencoder

    对于加深网络层数带来的问题,(gradient diffuse  局部最优等)可以使用逐层预训练(pre-training)的方法来避免 Stack-Autoencoder是一种逐层贪婪(Greedy ...

  8. php的session.serialize_handler

    php里面的session.serialize_handler用来设置php的session的序列化方式,默认值为php,及使用php的序列化与反序列化. 还有一个值为session.serializ ...

  9. 【英语】Bingo口语笔记(21) - 表达“请客吃饭”

  10. Oracle 11g RAC oc4j/gsd Offline

    Oracle 11g RAC中,发现oc4j以及gsd服务都处于offline状态,这是Oracle 11g RAC默认情形.即便如此,并不影响数据库的使用,因为 oc4j 是用于WLM 的一个资源, ...