题目:

报数

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

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. nginx服务器配置多域名

    nginx服务器支持配置多站点,我们可以通过配置子域名让你的一个域名下放置多个项目. 那么如何实现这个过程呢? 网络上的许多方案,有些写的过于繁杂,有些则是配置有误,或者说,有些配置项是要根据自己的主 ...

  2. linux前景到底怎么样啊?

    我就不长篇大论,举四个例子你看看. 1.目下最热最潮最流行的云计算技术的背后是虚拟化和网格技术,而虚拟化和网格技术基本是Linux的天下,目前虚拟化的三大家:Vmware,Xen,Hyper-V中,市 ...

  3. 感知器算法PLA

    for batch&supervised binary classfication,g≈f <=> Eout(g)≥0 achieved through Eout(g)≈Ein(g ...

  4. 使用go语言后的感受

    前两天我说过为了学习go语言去学习了一遍python,当我完成了python的学习后,昨天中午就去学习了go语言.以下简称之为golang. 我用的操作系统是windows xp,golang对xp还 ...

  5. js SVG

    Snap.svg Paths.js http://www.sitepoint.com/creating-animated-valentines-day-card-snap-svg/

  6. List<T>中Exists 和Contains的区别

    .net编码中,使用泛型List<>时,经常遇到这样的需求:新来一个Model对象,如果已有的List中没有这条数据,则把新对象Add到List中,否则不处理 判断已有的List中是否包含 ...

  7. Perceptron Learning Algorithm (PLA)

    Perceptron - 感知机,是一种二元线性分类器,它通过对特征向量的加权求和,并把这个”和”与事先设定的门槛值(threshold)做比较,高于门槛值的输出1,低于门槛值的输出-1.其中sign ...

  8. WPF 多线程处理(1)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 废话不多说,先上图: 多线程处理数据后在th ...

  9. 历时一周,unity3d+xtion打造我的第一个休闲体感小游戏《空降奇兵》

    1.游戏介绍 本游戏属于休闲小游戏,主要操作如下: 菜单控制:举起左手或右手,点击左边或者右边的菜单:挥动左手或右手,选择关卡: 操作方式:玩家跳跃,游戏中的伞兵从飞机开始降落:玩家通过控制伞兵的左右 ...

  10. struts2传map到前台出现的问题

    后台打印出的错: 2016-08-16 13:42:52.652 WARN  org.apache.struts2.json.JSONWriter     - JavaScript doesn't s ...