C++

 class Solution {
public:
/**
* @param n the nth
* @return the nth sequence
*/
string countAndSay(int n) {
// Write your code here
if ( == n) {
return "";
}
string pre = "";
for (int i = ; i < n; i++) {//从第2个(i=1)开始
char ch = pre[];
string cur = "";
int cnt = ;
for (int j = ; j < pre.size(); j++) {
if (pre[j] == ch) {
cnt ++;
} else {
cur = cur + itostr(cnt) + ch;
ch = pre[j];
cnt = ;
}
}
if (cnt != ) {//处理后边的字符
cur = cur + itostr(cnt) + ch;
}
pre = cur;
cur = "";
}
return pre; }
string itostr(int i) {//自定义int转string函数
char str[];
//itoa(str,i,10);->only support Windows
sprintf(str, "%d", i);//support any platforms
return str;
}
};

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

  1. [LintCode] Count and Say 计数和读法

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

  2. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  3. LintCode "Count of Smaller Number before itself"

    Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...

  4. LintCode Count 1 in Binary

    知识点 1. 整数的二进制表示法 2. 十进制和二进制的转换 http://baike.baidu.com/view/1426817.htm 3. 负整数的表示(原码,补码,反码) http://ww ...

  5. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  6. nodejs api 中文文档

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

  7. LintCode 391: Count Of Airplanes

    LintCode 391: Count Of Airplanes 题目描述 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例 对于每架飞机的起 ...

  8. lintcode :Count and Say 报数

    题目: 报数 报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数.如下所示: 1, 11, 21, 1211, 111221, ... 1 读作 "one 1" -> ...

  9. lintcode :Count 1 in Binary 二进制中有多少个1

    题目: 二进制中有多少个1 49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111 ...

随机推荐

  1. 一致性Hash算法说明

    本文章比较好的说明了一致性Hash算法的概念 Hash算法一般分为除模求余和一致性Hash1.除模求余:当新增.删除机器时会导致大量key的移动2.一致性Hash:当新增.删除机器时只会影响到附近的k ...

  2. SQL:获取中文周几

    ) = case DatePart(DW, GetDate()) then '周日' then '周一' then '周二' then '周三' then '周四' then '周五' then '周 ...

  3. python测试开发django-32.admin后台多对多关系ManyToManyField

    前言 一本书可以有多个作者,一个作者也可以写多本书,这种关系就是本篇要说的多对多关系. 这种场景在django里面数据库设计时候,需要用到ManyToManyField方法 多对多表设计 auther ...

  4. [Web 前端] td长度固定,内容过长,超过部分用省略号代替

    cp from : https://blog.csdn.net/bsh_csn/article/details/51829103 html的table表格中td长度固定,当内容过长时,超过部分用省略号 ...

  5. 【Java并发】JUC—ReentrantReadWriteLock有坑,小心读锁!

    好长一段时间前,某些场景需要JUC的读写锁,但在某个时刻内读写线程都报超时预警(长时间无响应),看起来像是锁竞争过程中出现死锁(我猜).经过排查项目并没有能造成死锁的可疑之处,因为业务代码并不复杂(仅 ...

  6. endnote插入参考文献后的对齐方式和缩进空格

    原文链接 https://jingyan.baidu.com/article/63acb44a3f0f6161fcc17ed0.html 1 2 3 4 5 6 7 分步阅读 1.endnote 导入 ...

  7. Anaconda需要添加的环境变量

    F:\Anaconda3 F:\Anaconda3\Scripts F:\Anaconda3\Library\bin

  8. [转]PHP之APC缓存详细介绍(学习整理)

    From : http://www.2cto.com/kf/201210/160140.html 1.APC缓存简介APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存 ...

  9. 关于android SDK安装Failed to fetch URL 一点思考

    最近SDK出问题了,然后在google下载了一个android-sdk-windows.rar,然后点击SDK Manager,结果一直不能刷新API Level,然后就开始在网上找了好多资料,解决这 ...

  10. 九度oj-1001-Java

    题目描述:     This time, you are supposed to find A+B where A and B are two matrices, and then count the ...