题目:

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

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. NOIp2018集训test-9-6(am)

    Problem A. divisor 发现x为k可表达一定可以表示成这种形式,如k=3,x=(1/3+1/2+1/6)x. 于是可以搜索k(k<=7)个1/i加起来等于1的情况,如果一个数是这些 ...

  2. NX二次开发-NXString转换为char*方法

    NX9+VS2012 #include <uf.h> #include <uf_drf.h> #include <NXOpen/Annotations_Note.hxx& ...

  3. C/C++:Windows编程—调用DLL程序的2种方法(转载)

    文章为转载,原文出处https://blog.csdn.net/qq_29542611/article/details/86618902 前言先简单介绍下DLL.DLL:Dynamic Link Li ...

  4. idea和eclipse快捷键对比(转)

    分类 功能点 Eclipse快捷键 IDEA快捷键 搜索 搜索文本 Ctrl + F Ctrl + F Ctrl + R 查找替换 Alt + P/A 逐个/全部替换 Alt + F3 查找当前选中词 ...

  5. Openstack Nova 源码分析 — 使用 VCDriver 创建 VMware Instance

    目录 目录 前言 流程图 nova-compute vCenter 前言 在上一篇Openstack Nova 源码分析 - Create instances (nova-conductor阶段)中, ...

  6. 简单实用的makefile

    简单的makefile 为了说明问题,就新建一组文件如下: 文件布局及运行结果: make clean 按目录归置 文件看起来是是清楚了,但是makefile写得揪心. 实用版 (1)Makefile ...

  7. Soldier and Number Game-素数筛

    Two soldiers are playing a game. At the beginning first of them chooses a positive integer n and giv ...

  8. A1095 Cars on Campus (30 分)

    Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...

  9. k8s 对接glusterfs存储

    service 与 endpoint 是通过namespace相同,name相同,相互关联的 创建endpoint [root@k8s-master glusterfs]# cat glusterfs ...

  10. Vue项目的配置项

    目录 Vue项目的配置项 配置项 加载全局css文件 加载全局js文件 store仓库的配置和简单用法 BootStrap环境和jQuery的配置 前端后端交互(CORS问题) axios配置项(前端 ...