题目:

报数

报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:

1, 11, 21, 1211, 111221, ...

1 读作 "one 1" -> 11.

11 读作 "two 1s" -> 21.

21 读作 "one 2, then one 1" -> 1211.

给定一个整数 n, 返回 第 n 个顺序。

样例

给定 n = 5, 返回 "111221".

注意

整数的顺序将表示为一个字符串

解题:

题目思路很清晰,按照高位到低位的顺序,统计相同数字的个数,并把a个b写成ab的形式,所以的连接在一起就是一个新数,下一个数利用同样的规律。

一个有意思的网站,Python程序来源。

Java程序:

public class Solution {
/**
* @param n the nth
* @return the nth sequence
*/
public String countAndSay(int n) {
// Write your code here
String oldString = "1";
while (--n>0){
StringBuilder sb = new StringBuilder();
char[] oldChars = oldString.toCharArray();
for(int i=0;i<oldChars.length;i++){
int count = 1;
while((i+1)<oldChars.length && oldChars[i]==oldChars[i+1]){
count++;
i++;
}
sb.append(String.valueOf(count) + String.valueOf(oldChars[i]));
}
oldString = sb.toString();
}
return oldString; }

总耗时: 7304 ms

程序来源

Python程序:

class Solution:
# @param {int} n the nth
# @return {string} the nth sequence
def countAndSay(self, n):
# Write your code here
p = ''
seq = [1]
m = n
while n>1:
q = ''
idx = 0
l = len(p)
while idx<l:
start = idx
idx = idx + 1
while idx<l and p[idx]==p[start]:
idx = idx + 1
q = q+str(idx - start) + p[start]
n, p = n -1 ,q
seq.append(int(p))
return str(seq[m-1])

总耗时: 312 ms

根据运行错误的结果,发现这个题目的测试数据只是 1 到9 这9个数,太小了吧。。。

lintcode :Count and Say 报数的更多相关文章

  1. 【LeetCode】Count and Say(报数)

    这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...

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

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

  3. 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 ...

  4. LintCode "Count of Smaller Number before itself"

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

  5. 38.Count and Say 报数

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

  6. LintCode Count 1 in Binary

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

  7. LintCode: Count and Say

    C++ class Solution { public: /** * @param n the nth * @return the nth sequence */ string countAndSay ...

  8. LeetCode "Count of Smaller Number After Self"

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

  9. C:数组

    数组.排序 关于排序 :参考 关于数组: 参考 求a[i][j]行与列的和然后求平均值 参考 二维数组使用指针的表示方法  参考 字符串数组:char  name [5][20] ={ {} , {} ...

随机推荐

  1. ExtJS实战 01——HelloWorld

    前言 Extjs5的发布已经有些日子了,目前的最新稳定版本是Extjs5.1.0,我们可以在官方网站进行下载.不过笔者今天访问得到的是502Bad Gateway,原因可能是sencha的nigix没 ...

  2. [大牛翻译系列]Hadoop(8)MapReduce 性能调优:性能测量(Measuring)

    6.1 测量MapReduce和环境的性能指标 性能调优的基础系统的性能指标和实验数据.依据这些指标和数据,才能找到系统的性能瓶颈.性能指标和实验数据要通过一系列的工具和过程才能得到. 这部分里,将介 ...

  3. MySQL语法语句大全

    一.SQL速成   结构查询语言(SQL)是用于查询关系数据库的标准语言,它包括若干关键字和一致的语法,便于数据库元件(如表.索引.字段等)的建立和操纵.   以下是一些重要的SQL快速参考,有关SQ ...

  4. iOS 点击return或者点击屏幕键盘消失

    //定义两个文本框 UITextField *textName; UITextField *textSummary; //点击return 按钮 去掉 -(BOOL)textFieldShouldRe ...

  5. 使用GDataXML解析XML文档

    转载自:http://blog.csdn.net/tangren03/article/details/7868246 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是 ...

  6. Win7开始菜单之【附件】不全解决方案

    Win7开始菜单之[附件]不全解决方案 1:打开你的[开始]菜单,转到附件,如果你发现你的附件里的“附件”不是那么全的话,如下图:来吧,我告诉你如何恢复它到最初的模样……哦,或许如果你不急于恢复的话, ...

  7. Lambda(2)

    Lambda表达式是匿名方法的超集,处理匿名方法有的功能外,还有其他的功能: 1.能够推测出参数的类型,无需显示声明 2.支持语句块和表达式作为方法体 Lambda表达式的书写方式: Lambda表达 ...

  8. 第一次比赛的 C题 (好后面才补的....) CodeForces 546B

    Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...

  9. Oracle使用%rowtype变量存储一行数据

    在Oracle中,%rowtype是用来存储一行数据的 语法: rowType_name table_name%rowtype rowType_name :变量名 table_name:指定的表名 具 ...

  10. Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space解决方法 问题描述 Exception ...