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.(Easy)

分析:

按照题目要求以此生成新串即可,注意第一个是串是1,即下标从1开始。

同时,复习使用to_string.

代码:

 class Solution {
public:
string countAndSay(int n) {
string s = "";
for (int i = ; i < n; ++i) {
int count = ;
string temp;
for (int j = ; j < s.size(); ++j) {
if (j == s.size() - || s[j] != s[j + ]) {
temp += to_string(count);
temp += s[j];
count = ;
}
else {
count ++;
}
}
s = temp;
}
return s;
}
};
 

LeetCode38 Count and Say的更多相关文章

  1. Leetcode字符串专题

    Leetcode38. Count and Say 分析:根据题意,数列的下一项就是统计上一项中每个数字出现的次数,理解清楚题意就很简单了 class Solution { public: strin ...

  2. nodejs api 中文文档

    文档首页 英文版文档 本作品采用知识共享署名-非商业性使用 3.0 未本地化版本许可协议进行许可. Node.js v0.10.18 手册 & 文档 索引 | 在单一页面中浏览 | JSON格 ...

  3. [Swift]LeetCode38. 报数 | Count and Say

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  4. C#中Length和Count的区别(个人观点)

    这篇文章将会很短...短到比你的JJ还短,当然开玩笑了.网上有说过Length和count的区别,都是很含糊的,我没有发现有 文章说得比较透彻的,所以,虽然这篇文章很短,我还是希望能留在首页,听听大家 ...

  5. [PHP源码阅读]count函数

    在PHP编程中,在遍历数组的时候经常需要先计算数组的长度作为循环结束的判断条件,而在PHP里面对数组的操作是很频繁的,因此count也算是一个常用函数,下面研究一下count函数的具体实现. 我在gi ...

  6. EntityFramework.Extended 实现 update count+=1

    在使用 EF 的时候,EntityFramework.Extended 的作用:使IQueryable<T>转换为update table set ...,这样使我们在修改实体对象的时候, ...

  7. 学习笔记 MYSQL报错注入(count()、rand()、group by)

    首先看下常见的攻击载荷,如下: select count(*),(floor(rand(0)*2))x from table group by x; 然后对于攻击载荷进行解释, floor(rand( ...

  8. count(*) 与count (字段名)的区别

    count(*) 查出来的是:结果集的总条数 count(字段名) 查出来的是: 结果集中'字段名'不为空的记录的总条数

  9. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

随机推荐

  1. C# rmi例子

    接口定义 实体定义,注意需要序列化 using System; namespace Interface { [Serializable] public class DataEntity { publi ...

  2. POJ 1811Prime Test(米勒拉宾素数测试)

    直接套用模板,以后接着用 这里还有一个素因子分解的模板 #include <map> #include <set> #include <stack> #includ ...

  3. C# JSON使用的常用技巧(一)

    获取JSON无格式的字符串: new JArray().ToString(Newtonsoft.Json.Formatting.None): 如: {"A":123,"B ...

  4. C#dll中无法找到c++dll中函数的入口

    刚试验了一下,老是c#中的dll无法找到c++dll中的函数的入口: 暂时发现有俩个原因 1,没有用extern “C” _declspec(dllexport),导致c#无法找到入口. 2,在c++ ...

  5. POJ2503——Babelfish

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  6. 用html5的canvas生成图片并保存到本地

    原文:http://www.2cto.com/kf/201209/156169.html 前端的代码: [javascript]  function drawArrow(angle)  {      ...

  7. TCP客户机-服务器

    1 僵尸进程 2 信号处理 信号: 1 由一进程发往另一进程 2 由内核发往某进程   僵尸状态: 父进程取回子进程的相关信息,进程的ID,终止状态,子进程的资源利用信息   编程时: 1 当派生子进 ...

  8. IE6/IE7下margin-bottom失效兼容解决办法及双倍边距问题

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-04-08) 一.IE6/IE7下margin-bottom失效兼容解决办法 1.用padding-bottom代替:2.在 ...

  9. 【css hack】正是我所找的,帮了大忙啊

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-03-05) 各个浏览器单独设置属性 IE6:能识别下划线 “_” 和 星号 “*“,不能识别 “!important”    ...

  10. wikioi 3038 3n+1问题

    题目描述 Description 3n+1问题是一个简单有趣而又没有解决的数学问题.这个问题是由L. Collatz在1937年提出的.克拉兹问题(Collatz problem)也被叫做hailst ...