题目

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,求出该序列对应的字符串。



规则如上图所示。

AC代码

class Solution {
public:
string countAndSay(int n) {
if (n <= 0)
return NULL; //n=1时,结果为"1"
string ret = "1";
if (n == 1)
return ret;
else
{
for (int i = 2; i <= n; i++)
ret = Count(ret);
}//else
return ret; } string Count(const string &str)
{
int size = strlen(str.c_str());
//保存结果
stringstream ret;
//保存标识字符
char flag = str[0];
//计算标识字符的出现次数
int count = 0 , i = 0;
while( i < size )
{
//临时循环位
int pos = i;
while (str[pos] == flag)
{
count++;
pos++;
}//while
ret << count << flag;
flag = str[pos];
count = 0;
//设置下一个循环位
i = pos;
}//for
return ret.str();
}
};

GitHub测试程序源码

LeetCode(38) Count and Say的更多相关文章

  1. Leetcode(38)-报数

    报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  "one 1&quo ...

  2. LeetCode(38): 报数

    Easy! 题目描述: 报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  &qu ...

  3. Leetcode(59)-Count Primes

    题目: Description: Count the number of prime numbers less than a non-negative number, n. 思路: 题意:求小于给定非 ...

  4. Leetcode(204) Count Primes

    题目 Description: Count the number of prime numbers less than a non-negative number, n. Credits: Speci ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(38)-Easyui-accordion+tree漂亮的菜单导航

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(38)-Easyui-accordion+tree漂亮的菜单导航 系列目录 本节主要知识点是easyui ...

  6. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  7. Windows Phone开发(38):动画之PointAnimation

    原文:Windows Phone开发(38):动画之PointAnimation PointAnimation也是很简单的,与前面说到的两个Animation是差不多的,属性也是一样的,如By.Fro ...

  8. Qt 学习之路 2(38):存储容器

    Qt 学习之路 2(38):存储容器 豆子 2013年1月14日 Qt 学习之路 2 38条评论 存储容器(containers)有时候也被称为集合(collections),是能够在内存中存储其它特 ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. X86 Linux 下 SIGBUS 总结

    SIGBUS 在 x86 Linux 上并不多见,但一旦出现,其调用堆栈常常让人摸不着头脑,加之信号问题各平台系统间差异较大,更让人难以理清,这里稍微总结一下 x86 Linux 上大概有哪些情形会触 ...

  2. Luogu P1197 [JSOI2008]星球大战 By cellur925

    题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧 ...

  3. docker速记

    1.docker:一个轻量级的虚拟机.是一个容器 2.Linux系统包括—RedHat(商业版).Centos.Ubuntu 3.docker比作码头的集装箱,image镜像就是基石,images类似 ...

  4. linux 正确的关机方法

    正确的关机方法 1. 查看系统的使用状态 执行who命令或者netstat -a ,要查看后台执行的程序可以执行“ps -aux” 2. 正确的关机命令 1)将内存中数据同步写入磁盘:sync,这个命 ...

  5. (二)python高级特性

    一.切片 >>> L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] 对这种经常取指定索引范围的操作,用循环十分繁琐,因此,Python ...

  6. 【LeetCode】树的遍历

    非递归中序遍历: 思路:注释 vector<int> inorderTraversal(TreeNode* root) { vector<int>ret; if(root == ...

  7. 435 Non-overlapping Intervals 无重叠区间

    给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意:    可以认为区间的终点总是大于它的起点.    区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...

  8. RabbitMQ七:交换机类型Exchange Types--Fanout 介绍

    前言 最新版本的RabbitMQ有四种交换机类型,分别是Direct exchange.Fanout exchange.Topic exchange.Headers exchange. 其中之前我们用 ...

  9. mysql之流程控制

    目录 分支结构 循环结构 分支结构: 1.if condition then [statement] elseif condition then [statement] else [statement ...

  10. php中 mysql 插入特殊字符(手机端的emoji表情)出现异常

    今天在用mysql存储从微信服务器拉来的数据,出现插入数据异常,报 Incorrect string value: '\xF0\x9F\x98\x97\xF0\x9F 的错误. 最终在网上查了一下,有 ...