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. 对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解

    依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调 ...

  2. linux内核netfilter模块分析之:HOOKs点的注册及调用

    转自;http://blog.csdn.net/suiyuan19840208/article/details/19684883 -1: 为什么要写这个东西?最近在找工作,之前netfilter 这一 ...

  3. java初始化ArrayList

    初始化ArrayList我们一般这样写:ArrayList<String> places = new ArrayList<String>();places.add(" ...

  4. 自定义PreferenceActivity和PreferenceFragment的样式

    感谢:http://blog.csdn.net/luck_apple/article/details/7064004 这篇文章讲的是如何定义fragment的样式,基本布局都是从源码中弄过来的.通过设 ...

  5. 用ArrayAdapter来创建Spinner(自定义布局、默认布局、动态内容、静态内容)

             android:dropDownWidth 下拉列表宽度 android:dropDownHorizontalOffset 下拉列表距离左边的距离 android:dropDownV ...

  6. [Web 前端 ] 还在用浮动吗?CSS flex布局你了解多少?

    cp from : https://blog.csdn.net/wwwxuewen/article/details/80859764 传统的布局:围绕盒子模型(border.margin.paddin ...

  7. Netty 4.0.0.CR6 发布,高性能网络服务框架

    Netty 4.0 发布第 6 个 RC 版本,该版本值得关注的改进有: SslHandler and JZlibEncoder now works correctly. (#1475 and #14 ...

  8. nfd指令的详细说明

    在eterm上执行NFD:SHAPEK/CA*OW指令,返回如下: LN CXR OW RT FBC/TC RBD MIN/MAX TRVDATE R 01 CA 450.00 U U 00D/00D ...

  9. solr4.7配置(ik-analyzer)

    环境: windows server 2003 sp2 x86 tomcat8.0 solr-4.7.2 IK Analyzer 2012FF_hf1 ————————————华丽的分割线—————— ...

  10. Js组件的一些写法

    首先看下Prototype里的写法: var Class = { create: function() { return function() { this.init.apply(this, argu ...