题目

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. nmcli 学习小结

    nmcli 是Redhat提供的网络配置编辑工具, 它可直接编辑/etc/sysconfig/network-scripts/ifcfg-xxx , 它是NetworkManager服务的客户端工具, ...

  2. #undef及其用法

    简    介 在后面取消以前定义的宏定义 在此程序中,我们将取消在先前程序中对预处理器的定义. #include <stdio.h> int main( void ) { #define ...

  3. Python中处理日期时间库的使用方法(转载)

    <本文来自公众号“大邓带你玩python”,转载> 用百分之20时间,学会解决百分之80的问题. 常用的库有time.datetime.其中datetime库是对time库的封装,所以使用 ...

  4. Windows 程序设计 笔记

    知识点 双字节字符集和Unicode字符集有何区别?采用双字节字符集有何问题 双字节字符集(DBCS)编码是0-255,DBCS含有1字节代码与2字节代码,而Unicode是统一的16位系统,这样就允 ...

  5. python网络爬虫之四简单爬取豆瓣图书项目

    一.爬虫项目一: 豆瓣图书网站图书的爬取: import requests import re content = requests.get("https://book.douban.com ...

  6. Hello!六月

    把这里当做记事本应该没人介意吧: 太忙了!六月! ACM: 背包九讲

  7. java 字符串截取的几种方式

    1.split()+正则表达式来进行截取. 将正则传入split().返回的是一个字符串数组类型.不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时. String str = " ...

  8. 面相切面编程AOP以及在Unity中的实现

    一.AOP概念 AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术.它是 ...

  9. Struts2------拦截器和标签库和注解开发

    一.解析Struts2源码中拦截器的执行 客户端请求Action,执行前端控制器,在前端控制器内部创建了Action的代理类,调用代理类的execute方法,在execute方法内部执行ActionI ...

  10. Asp.Net控件的客户端命名

    我们在用ASP.NET写出来的网页,用浏览器来查看生成的客户端代码的时候经常看到这样的代码:GridView1_ctl101_WebUserControl1_webuserControlButton, ...