题目:

报数

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

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. Bootstrap轮播(carousel)插件中图片变形的终极解决方案——使用jqthumb.js

    在顶求网的首页中我使用了BootStrap的轮播(carousel)插件来展示文章中的图片.我在程序中自动抓取文章的第一张图片作为该轮播控件中要显示的图片,由于文章的图片大小不一,而轮播插件的大小基本 ...

  2. [DevExpress]ChartControl之时间轴示例

    关键代码: using System; using System.Data; using System.Windows.Forms; using DevExpress.XtraCharts; name ...

  3. iOS实现图片的缩放和居中显示

    直接上代码 // // MoveScaleImageController.h // MoveScaleImage // // Created by on 12-4-24. // Copyright ( ...

  4. 提高Linux安全性--hosts.allow, hosts.deny 文件修改方法

    有一种办法来提高Linux安全性--修改 hosts.allow , hosts.deny 这2个文件来配置 允许某个ip访问, 或者禁止访问. 可以通过这种方式设置限制 sshd 的远程访问, 只允 ...

  5. ASP.NET——拒绝访问。 (异常来自HRESULT:0x80070005 (E_ACCESSDENIED))

    运行ASP.NET网站的时候出现下面这个问题,这个问题如图: 这个问题的解决方法为: 运行dcomcnfg 点 组件服务->服务->电脑->我的电脑->DCOM 配置 找到&q ...

  6. iOS学习笔记之typedef

    typedef unsigned long long weiboId; typedef 定义一个使用方便的类型,谓之为“宏定义“. unsigned long long 是一种无符号的长长整型.本应该 ...

  7. per-project basis

    Of course, HSQLDB connection parameters should be stored on a per-project basis, instead of only onc ...

  8. VB 思维导图总结(二)

    第六章.过程... 第七章.菜单工具栏... 第八章.文件系统处理.. 第九章.键盘和鼠标事件...

  9. 64bit Ubuntu, Android AAPT, R.java

    Ubuntu 13.10 aapt: error while loading shared libraries: libstdc++.so.6: cannot open shared object f ...

  10. 设计模式Builder(建造者)模式

    1.出现原因 在软件系统中,有时候会面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成:由于需求的变化,这个复杂的对象的各个部分可能面临着剧烈的变化,但是把他们组合在一起的算法 ...