题目链接

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.


题目描述的不是很清楚,其实就是第i+1个字符串是第i个字符串的读法,第一字符串为 “1”

比如第四个字符串是1211,它的读法是 1个1、1个2,2个1,因此第五个字符串是111221。

第五个字符串的读法是:3个1、2个2、1个1,因此第六个字符串是312211                  本文地址

......

简单的模拟就可以。

 class Solution {
public:
string countAndSay(int n) {
if(n < )return "";
string prev = "";
for(int i = ; i <= n; i++)
{
char curChar = prev[];
int times = ;//curChar 出现的次数
string tmpstr;
prev.push_back('#');//处理边界条件
for(int k = ; k < prev.size(); k++)
{
if(prev[k] == curChar)
times++;
else
{
tmpstr += to_string(times);
tmpstr.push_back(curChar);
curChar = prev[k];
times = ;
}
}
prev = tmpstr;
}
return prev;
}
};

其实我们可以发现字符串中永远只会出现1,2,3这三个字符,假设第k个字符串中出现了4,那么第k-1个字符串必定有四个相同的字符连续出现,假设这个字符为1,则第k-1个字符串为x1111y。第k-1个字符串是第k-2个字符串的读法,即第k-2个字符串可以读为“x个1,1个1,1个y” 或者“*个x,1个1,1个1,y个*”,这两种读法分别可以合并成“x+1个1,1个y” 和 “*个x,2个1,y个*”,代表的字符串分别是“(x+1)11y” 和 "x21y",即k-1个字符串为“(x+1)11y” 或 "x21y",不可能为“x1111y”.

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3776356.html

LeetCode:Count and Say的更多相关文章

  1. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  2. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  3. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  4. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  5. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  6. [LeetCode] Count Primes 质数的个数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  7. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  8. LeetCode Count of Smaller Numbers After Self

    原题链接在这里:https://leetcode.com/problems/count-of-smaller-numbers-after-self/ 题目: You are given an inte ...

  9. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

  10. LeetCode——Count and Say

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

随机推荐

  1. 解决IE兼容模式问题

    IE浏览器从IE8开始添加了兼容模式,开启后会以低版本的IE进行渲染.在浏览网页时候会出现网页显示问题,于是可以在html中加入以下代码来使IE使用固定的渲染模式: <metahttp-equi ...

  2. iOS之2016面试题三

    1.OC内存管理机制 1).当你使用new,alloc和copy方法创建一个对象时,该对象的保留计数器值为 1.当你不再使用该对象时,你要负责向该对象发送一条release或autorelease消息 ...

  3. 关于watir-webdriver中文乱码问题

    require 'watir-webdriver' require 'iconv' cov = Iconv.new( 'gbk', 'utf-8') b = Watir::Browser.new b. ...

  4. JVM-操作码助记符

    整理如下,用于以后查找: Opcode Mnemonics Note Constants 0x00 nop 无动作 0x01 aconst_null 把 null 推到操作数栈 0x02 iconst ...

  5. 软件工程--界面UI 的原型设计

    经过今天的讨论, 我们组初步给出了设计方案. 安装了一晚上的 mockupBuilder 终于绘制了几份界面的原型图, 这里需要吐槽下 mockupBuilder, 这个软件很好用, 不过网页版和单机 ...

  6. CLR垃圾回收的设计

    作者: Maoni Stephens (@maoni0) - 2015 附: 关于垃圾回收的信息,可以参照本文末尾资源章节里引用的垃圾回收手册一书. 组件架构 GC包含的两个组件分别是内存分配器和垃圾 ...

  7. 职业规划:管理vs技术

    "每个人都身怀天赋,但如果用会不会爬树能力来评判一只鱼,那它这辈子都会觉得自己是条蠢鱼" - 阿尔伯特.爱因斯坦 我想我为这篇博客已经准备了很长时间.但是看起了我还一直挣扎我该往哪 ...

  8. Linux下开启关闭SeLinux

    SELinux (Security-Enhanced Linux) in Fedora is an implementation of mandatory access control in the ...

  9. oradebug/strace/pstack等分析数据库性能问题系列一

    对于性能问题或者一些比较奇怪妖异的问题,有很多点可以着手去分析. 准备写一个系列关于用ash/dba_hist_active_sess_history,用oradebug,用linux命令strace ...

  10. SQL Update:使用一个表的数据更新另一张表

    表结构 功能 用表B的数据(mc列)更新表A的mc列 SQL Server update A SET A.mc = b.mc FROM A ,B WHERE A.bmbh = B.bmbh and A ...