LeetCode 38.报数(Python3)
题目:
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下:
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)的更多相关文章
- [leetcode] 38. 报数(Java)(字符串处理)
38. 报数 水题 class Solution { public String next(String num) { String ans = ""; int i = 0; wh ...
- Leetcode 38.报数 By Python
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1" ...
- C#刷遍Leetcode面试题系列连载(2): No.38 - 报数
目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...
- Leetcode题库——38.报数
@author: ZZQ @software: PyCharm @file: countAndSay.py @time: 2018/11/9 14:07 说明:报数序列是一个整数序列,按照其中的整数的 ...
- 【leetcode算法-简单】38. 报数
[题目描述] 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作 "one 1&qu ...
- 【LeetCode 38】报数
题目链接 [题解] 模拟题 [代码] class Solution { public: string inttostr(int x){ string temp=""; while ...
- [LeetCode] 38. Count and Say 计数和读法
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Java实现 LeetCode 38 外观数列
38. 外观数列 「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述.前五项如下: 1 11 21 1211 111221 1 被读作 "one 1" ...
- LeetCode - 38. Count and Say
38. Count and Say Problem's Link ------------------------------------------------------------------- ...
随机推荐
- 依赖背包变形——hdu4003
思维性比较强,代码挺简单的,dp[u][j]表示在u子树下安排j个机器人,让其不回u 注意转移时的初始值 /* dp[u][j]为在子树u有j个机器人不回来 */ #include<bits/s ...
- flume的安装和使用
1.下载 [linyouyi@hadoop01 software]$ wget https://mirrors.aliyun.com/apache/flume/1.9.0/apache-flume-1 ...
- NX二次开发-UFUN获取当前显示部件的TAG,UF_PART_ask_display_part
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_part.h> UF_initialize ...
- [转]gulp打包工具总结
与grunt类似,gulp也是构建工具,但相比于grunt的频繁IO操作,gulp的流操作能更快更便捷地完成构建工作.gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级 ...
- random,time,sys,os,序列化模块
random模块(随机数模块) 取随机小数: random.random() 取0-1之间的小数 random.uniform(x, y) 取x-y之间的小数 取随机整数: random.randin ...
- [转]C++ 使用Makefile文件
//*********list class.h**********class tdate {private:int month;int day;int year;public:tdate();tdat ...
- opencv——基础篇
一 . opencv是什么及其作用? OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux.Windows.Android和Mac OS操作系统上.它轻量级而且高效— ...
- 28-Ubuntu-远程管理命令-02-查看网卡的配置信息
命令 功能 ifconfig 查看网卡配置信息 ifconfig | grep inet 查看网卡对应的IP地址 ping 127.0.0.1 检测本地网卡是否正常 ping IP地址 检测到目标 ...
- codeforces round#524 C. Masha and two friends /// 矩形切割
题目大意: 给定n行m列的黑白棋盘如下 给定矩形的左下点x1 y1和右上点x2 y2将这个区域都涂成白色 再给定矩形的左下点x3 y3和右上点x4 y4将这个区域都涂成黑色 求最后棋盘内有分别多少个白 ...
- 【POJ】2492 A Bug's Life
题目链接:http://poj.org/problem?id=2492 题意:给你n个虫子,m组实验.让你帮科学家找一下有没有虫子是同性恋. 题解:假设x是一个性别,x+n为另一个性别.如果在同性的集 ...