lintcode :Count and Say 报数
题目:
报数指的是,按照其中的整数的顺序进行报数,然后得到下一个数。如下所示:
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 报数的更多相关文章
- 【LeetCode】Count and Say(报数)
这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...
- [LintCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- 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 ...
- LintCode "Count of Smaller Number before itself"
Warning: input could be > 10000... Solution by segment tree: struct Node { Node(), left(nullptr), ...
- 38.Count and Say 报数
[抄题]: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 1 ...
- LintCode Count 1 in Binary
知识点 1. 整数的二进制表示法 2. 十进制和二进制的转换 http://baike.baidu.com/view/1426817.htm 3. 负整数的表示(原码,补码,反码) http://ww ...
- LintCode: Count and Say
C++ class Solution { public: /** * @param n the nth * @return the nth sequence */ string countAndSay ...
- LeetCode "Count of Smaller Number After Self"
Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...
- C:数组
数组.排序 关于排序 :参考 关于数组: 参考 求a[i][j]行与列的和然后求平均值 参考 二维数组使用指针的表示方法 参考 字符串数组:char name [5][20] ={ {} , {} ...
随机推荐
- 一款仿PBA官网首页jQuery焦点图的切换特效
一款仿PBA官网首页jQuery焦点图的切换特效,非常的简单大方, 在对浏览器兼容性的方面做了不少的功夫.IE6也勉强能过去. 还是一款全屏的焦点图切换特效.大气而清新.很适合简介大方的网站. 下图还 ...
- jexus 配置 学习
http://www.linuxdot.net/ 1.禁止或允许某IP或IP段访问网站 A.只允许某些IP地址访问网站(白名单功能) 默认情况下,允许所有IP地址访问.如果手工设置IP地址白名单, 那 ...
- 高德amap 根据坐标获取的地址信息
高德地理逆地理编码接口List<List<Address>> lists = coder.getFromLocation(33.00, 116.500, 3, 3, 3, 50 ...
- jquery数组之存放checkbox全选值示例代码
使用jquery数组可以存放checkbox全选值,下面有个不错的示例,感兴趣的朋友可以参考下. 复制代码代码如下: <input type="checkbox" id=&q ...
- php 文件上传类 实例分享
最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...
- WINDOWS下PHP 的pear DB的安装(本地环境:PHP5.4.15+Apache+mysql)
因为需要安装phpunit,要先装pear,网上的教程大多数是以双击go-pear.bat开始,但是我安装的php文件夹里压根没有这个文件. 经过几次搜索之后终于找到了办法. 解决步骤如下: 1.下载 ...
- nginx+keepalived双主高可用负载均衡
实验环境及软件版本:CentOS版本: 6.6(2.6.32.-504.el6.x86_64)nginx版本: nginx-1.6.3keepalived版本:keepalived-1.2.7 主LB ...
- grappelli美化django的admin页面
开始用admin时候,觉得它的页面实在...宁愿自己写modules,多费点时间 grappelli可以把admin变得非常美观,配置起来也很简单 第一步,先下载grappelli,搜索一下,wind ...
- 【WPF学习日记——[DevExpress]】GridControl 行中使用按钮
想到的办法都试了,只有这个能用,不一定是最好的,但却是自己能想到的,记录一下. <dxg:GridColumn Header="操作" Width="134&quo ...
- linux内核源码注解
轻松学习Linux操作系统内核源码的方法 针对好多Linux 爱好者对内核很有兴趣却无从下口,本文旨在介绍一种解读linux内核源码的入门方法,而不是解说linux复杂的内核机制:一.核心源程序的文件 ...