题目

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. VS2010编译错: #error : This file requires _WIN32_WINNT to be #defined at least to 0x0403...的解决方法

        最近拿到一个别人的工程,是使用VS.net创建的,而我的机器上只有vs2010,于是用自带的转换工具将它转换成vs2010的工程,转换之前我就很担心,怕转换完后会出问题,但是没有办法,我实在是 ...

  2. MFC中利用CString和Format成员函数将数字格式化输出

    str.Format("格式控制字符串”,输出列表): 格式控制字符串包括格式字符串和非格式字符串,用双引号括起来.其中非格式字符串原样输出. 格式字符串是以%开头的字符串:%[标识][输出 ...

  3. [Usaco2006 Open]The Climbing Wall 攀岩

    Description One of the most popular attractions at the county fair is the climbing wall. Bessie want ...

  4. centOS下安装JDK1.8.60,glassfish4.1.1以及MySQL

    一.安装环境 操作系统 Windows7 Enterprise 64位 需要用到的软件 JDK:jdk-8u60-linux-x64.rpm Glassfish: Glassfish4.1.1.zip ...

  5. ORA-14074: partition bound must collate higher than that of the last partition

    There is a error happen in crotab: CREATE parttion report ORA-14074:ORA-14074: partition bound must ...

  6. 451 Sort Characters By Frequency 根据字符出现频率排序

    给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...

  7. 记录一下java在后端用request来判断请求类型

    这几天看代码,看到这么一个操作. String requestType = request.getHeader("X-Requested-With");  于是各种查找   这里记 ...

  8. AJPFX总结JAVA基本数据类型

    1:关键字(掌握)        (1)被Java语言赋予特定含义的单词        (2)特点:                全部小写.        (3)注意事项:              ...

  9. Python 设计模式--简单工厂模式

    简单工厂模式(Factory Pattern)是一种创建型的设计模式,像工厂一样根据要求生产对象实例. 特点:根据不同的条件,工厂实例化出合适的对象. <大话设计模式>中实例:四则运算计算 ...

  10. VUE 入坑系列 一 基础语法

    html代码 <div id="app"> {{message}} </div> JavaScript代码 var vm = new Vue({ el: & ...