leetcode第38题:报数

这是一道简单题,但是我做了很久,主要难度在读题和理解题上。
思路:给定一个数字,返回这个数字报数数列。我们可以通过从1开始,不断扩展到n的数列。数列的值为前一个数列的count+num,所以我们不断叠加来完成。
class Solution:
def countAndSay(self, n: int) -> str:
# 第一个值直接赋予
pre_num = ""
for i in range(1,n):
# 用next_num 记录当前序列,num为前一个序列的第一个值
next_num,num,count="",pre_num[0],1
for j in range(1,len(pre_num)):
# 如果num和前一个序列的下一个值相等,则将count +1
if num==pre_num[j]:
count+=1
else:
# 如果不等,将当前count +num 写入next_num,并将count置为1,
# num取 当前pre_num的值
next_num += str(count) + num
count =1
num =pre_num[j]
next_num += str(count) + num
pre_num = next_num
return pre_num
leetcode第38题:报数的更多相关文章
- Leetcode(38)-报数
报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 "one 1&quo ...
- leetcode第38题--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
- 【LeetCode算法-38】Count and Say
LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- leetcode第37题--Count and Say
题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode的刷题利器(伪装到老板都无法diss你没有工作)
在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
随机推荐
- Neo4j图形数据库备份
Neo4j图形数据库备份 backup.sh文件 nowtime=`date +"%Y-%m-%d_%H_%M"` #原文件路径 sourcepath='/home/neo4j/n ...
- git commit 后 尚未push到远程,撤销commit
执行commit后,还没执行push时,想要撤销这次的commit,该怎么办? 解决方案: 使用命令: git reset --soft HEAD^ 这样就成功撤销了commit,如果想要连着add也 ...
- Ka/ Ks|同义替换的三种路径|kaks_Calculator|
生命组学 研究old gene 和 young gene CAI选择信号:CGmutation信号 Neutrality plot:CG3与GC1.GC2的关系:平:mutation:正相关:sele ...
- Eclipse 配置spring boot pom.xml第1行报错的两种解决办法
现象 通过spring boot项目导入eclipse后,pom.xml文件的第一行总是报错.这里使用的spring版本是2.1.5,2.1.4以前的版本等其他版本的spring没有这个问题. 解决办 ...
- frp内网穿透,centos7+frp成功样例
准备工作: 阿里云服务器一台,备案域名一个,本地服务器一台(本人用的虚拟机centos7) frp文件:frp_0.22.0_linux_amd64.tar.gz 链接:https://pan.bai ...
- Java中常用的API(四)——其他
前面说三篇文章分别介绍了Object.String.字符缓冲类的API,接下来我们简要介绍一下其他常用的API. 1.System System类用于获取各种系统信息,最为常用的是: System.o ...
- Dynamics CRM - 利用 JavaScript 打开指定 entity 的新建窗口并传递需要的参数
由于业务逻辑需要,需要从某个 Entity 的 Record 中弹出其他 Entity 的创建窗口,并将当前 Entity 中的某些值传递到新打开的窗口中,具体的 JS 代码如下: //定义一个参数对 ...
- Docker部署freeswitch
1. clone配置文件到本地服务器 git clone https://github.com/BetterVoice/freeswitch-container.git 相关Dockerfile如下: ...
- uploadifive使用笔记
官网地址:http://www.uploadify.com/ uploadifive 是基于H5开发,所以支持移动端和PC端 <input type="file" name= ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...