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ㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
随机推荐
- 3.GIT常用命令
往仓库里面添加文件 将新的文件放到暂存区 git add 文件名(多个文件用空格分隔) git commit -m '说明内容' git commit --amend 此修改一般用于未push之前修改 ...
- Linux-IO多路复用select函数实践
#include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/time.h ...
- 第22章 Makefile基础
一.自动处理头文件的依赖关系 在Makefile中插入如下代码: include $(sources:.c=.d) %.d: %.c set -e; rm -f $@; \ $(CC) -MM $(C ...
- UVA 10273
我是用暴力过的,虽然网上说刘汝佳出的这道题考的是堆,我不太懂,..用暴力时间复杂度高一些,但是一样能过 所要注意的就是周期问题,因为只要同时存在某一天超过一头牛产奶量最小,就不会杀牛,而每头牛的周期和 ...
- MyBatis从入门到精通(第5章):MyBatis代码生成器
jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.Eclipse Version: 2019-12 M2 (4.14.0) MyBatis从入门到精通(第5章):MyBatis代码 ...
- fscanf的用法
fscanf用于读取字符串数据流,遇到空白字符(空格' '; 制表符'\t'; 新行符'\n')就停止,若要读取完整的一行数据,可以使用格式控制("%[^\n]%*c"),或者使用 ...
- mysql Communications link failure Last packet sent to the server was X ms ago
想必大家在用MySQL时都会遇到连接超时的问题,如下图所示: 就是这个异常(com.mysql.jdbc.exceptions.jdbc4.Communication***ception:Commun ...
- 吴裕雄--天生自然 PYTHON3开发学习:XML 解析
<collection shelf="New Arrivals"> <movie title="Enemy Behind"> <t ...
- BTree非递归
preorder void PreOrder(BTNode* b) { BTNode* p = b; SqStack* st; InitStack(st); if (b != NULL) { Push ...
- springmvc register过程
福建SEO:首先在AbstractHandlerMethodMapping中,在afterPropertiesSet这个钩子函数中,先初始化handlerMethods. 在detectHandler ...