题目:

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

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. DELPHI 数据库操作

    DELPHI 把数据库中的数据转换成XML格式 function ReplaceString(AString: string): string; begin Result := StringRepla ...

  2. NX二次开发-创建圆弧(圆心-半径)UF_CURVE_create_arc_center_radius

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_modl.h> UF_initializ ...

  3. Unity 之旋转

    代码如下: bool RotateDelta(Vector3 direction) { direction.y = ; if (direction == Vector3.zero) return tr ...

  4. spark-sql createOrReplaceTempView 和createGlobalTempView区别

    在讲解 createOrReplaceTempView 和createGlobalTempView的区别前,先了解下Spark Application 和  Spark Session区别 Spark ...

  5. matplotlib画图出现乱码情况

    python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注 ...

  6. C++数据类型之实型(浮点型)&科学计数法

    实型(浮点型) **作用**:用于==表示小数== 浮点型变量分为两种: 1. 单精度float 2. 双精度double 两者的**区别**在于表示的有效数字范围不同. float类型数据,需在数据 ...

  7. 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型

    题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...

  8. springboot中参数校验

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  9. 科普帖:深度学习中GPU和显存分析

    知乎的一篇文章: https://zhuanlan.zhihu.com/p/31558973 关于如何使用nvidia-smi查看显存与GPU使用情况,参考如下链接: https://blog.csd ...

  10. css布局-瀑布流的实现

    一.基本思路 1.先看最终的效果图: 2.实现原理:通过position:absolute(绝对定位)来定位每一个元素的位置,并且将当前列的高度记录下来方便下一个dom位置的计算 二.代码实现 1.版 ...