题目:

报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:

1.     1
2. 11
3. 21
4. 1211
5. 111221

1 被读作  "one 1"  ("一个一") , 即 11
11 被读作 "two 1s" ("两个一"), 即 21
21 被读作 "one 2",  "one 1" ("一个二" ,  "一个一") , 即 1211

给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n 项。

注意:整数顺序将表示为一个字符串。

示例 1:

输入: 1
输出: "1"

示例 2:

输入: 4
输出: "1211"

解答:

方法一:

class Solution:
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
temp = '1'
new_temp = ''
for i in range(n):
if i == 0:
continue
index = 0
for j in range(len(temp)):
if j == len(temp) - 1 or temp[j] != temp[j + 1]:
new_temp += str(j + 1 - index) + temp[index]
index = j + 1
temp = new_temp
new_temp = ''
return temp

  

LeetCode 38.报数(Python3)的更多相关文章

  1. [leetcode] 38. 报数(Java)(字符串处理)

    38. 报数 水题 class Solution { public String next(String num) { String ans = ""; int i = 0; wh ...

  2. Leetcode 38.报数 By Python

    报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1" ...

  3. C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

    目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...

  4. Leetcode题库——38.报数

    @author: ZZQ @software: PyCharm @file: countAndSay.py @time: 2018/11/9 14:07 说明:报数序列是一个整数序列,按照其中的整数的 ...

  5. 【leetcode算法-简单】38. 报数

    [题目描述] 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作  "one 1&qu ...

  6. 【LeetCode 38】报数

    题目链接 [题解] 模拟题 [代码] class Solution { public: string inttostr(int x){ string temp=""; while ...

  7. [LeetCode] 38. Count and Say 计数和读法

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  8. Java实现 LeetCode 38 外观数列

    38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...

  9. LeetCode - 38. Count and Say

    38. Count and Say Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. 思维题+贪心——牛客多校第一场C

    /* 给定一组n维向量 A=(a1/m,a2/m,a3/m ... an/m), 求另一个n维向量 P=(p1,p2,p3...pn),满足sum{pi}=1,使得ans=sum{(ai/m-pi)^ ...

  2. NX二次开发-UFUN关闭本地文本文档uc4540

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_cfi.h> 5 #include <uf_ui.h> 6 7 us ...

  3. c++ pb_ds库,实现 红黑树,Splay

    C++ pb_ds库 #include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/tree_policy.hpp> ...

  4. 使用python和tushare查询股票历史名称变更记录

    接口:namechange 描述:历史名称变更记录 注:tushare库下载和初始化教程,请查阅我之前的文章 输入参数 名称       |       类型       |       必选    ...

  5. POJ 3667 线段树区间合并裸题

    题意:给一个n和m,表示n个房间,m次操作,操作类型有2种,一种把求连续未租出的房间数有d个的最小的最左边的房间号,另一个操作时把从x到x+d-1的房间号收回. 建立线段树,值为1表示未租出,0为租出 ...

  6. mysql UDF提权 sys_bineval

    介绍: 执行shellcode – sys_bineval sqmapl自带udf.dll中存在函数’sys_bineval,执行shellcode – sys_bineval 利用: MSF生成sh ...

  7. python_django_models模块中的查询

    查询集:表示从数据库获取的对象集合,查询集可以有多个过滤器,过滤器就是一个函数(方法),基于所给参数限制查询集结果 从sql角度来说,查询集和select等价,过滤器和where等价 查询集特点: 惰 ...

  8. [190308]Ubuntu 安装完之后,安装的软件小记

    install software vim sudo apt-get install -y vim Typora command copy from Typora website # or run: # ...

  9. yii2 vendor/bower/jquery/dist not exist

    查看 vendor 文件夹,只有bower-asset文件夹 手动修改 bower-asset 为bower 倒也可以,yii2项目每次 composer install 成功之后,每次重命名这个文件 ...

  10. Windows 隐藏控制台

    #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"& ...