这是一道简单题,但是我做了很久,主要难度在读题和理解题上。

思路:给定一个数字,返回这个数字报数数列。我们可以通过从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题:报数的更多相关文章

  1. Leetcode(38)-报数

    报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作  "one 1&quo ...

  2. leetcode第38题--Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  3. 【LeetCode算法-38】Count and Say

    LeetCode第38题 The count-and-say sequence is the sequence of integers with the first five terms as fol ...

  4. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  5. leetcode第37题--Count and Say

    题目:(据说是facebook的面试题哦) The count-and-say sequence is the sequence of integers beginning as follows:1, ...

  6. 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 + ...

  7. 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 ...

  8. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

  9. LeetCode每天一题之两数之和

    这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...

随机推荐

  1. selenium浏览器内核监测处理

    一.代码 from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions option = Chr ...

  2. UVA 10806 最小费用最大流

    终于可以写这道题的题解了,昨天下午纠结我一下下午,晚上才照着人家的题解敲出来,今天上午又干坐着想了两个小时,才弄明白这个问题. 题意很简单,给出一个无向图,要求从1 到 n最短路两次,但是两次不允许经 ...

  3. 干货 | IP高防使用配置

    一.知识简介 DoS(Denial of Service),即拒绝服务攻击.该攻击是利用目标系统网络服务功能缺陷或者直接消耗其系统资源,目的是使该目标客户的系统不可用,无法提供正常的服务. DDoS( ...

  4. Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). BitcoinJSONRPCClient异常、及其他异常

    1.异常信息 Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path) ...

  5. MyBatis从入门到精通(第4章):MyBatis动态SQL【foreach、bind、OGNL用法】

    (第4章):MyBatis动态SQL[foreach.bind.OGNL用法] 4.4 foreach 用法 SQL 语句中有时会使用 IN 关键字,例如 id in (1,2,3).可以使用 ${i ...

  6. sublime text2设置快捷键打开浏览器

    1 编辑一个py文件,内容如下: import sublime, sublime_plugin import webbrowser url_map = { 'C:\\server\\www\\' : ...

  7. Python上楼梯

    假设一段楼梯共n(n>1)个台阶,小朋友一步最多能上3个台阶,那么小朋友上这段楼梯一共有多少种方法. (小朋友真的累,我选择电梯) 大体思路用到了递归,假如说楼梯有12阶,那么11阶时有只有一种 ...

  8. luffy课程表的创建-支付宝API-购买服务器

    课程组件 <template> <div class="course"> <Header></Header> <div cla ...

  9. ZJNU 1534 - Problem Robot--高级

    因为是从(0,0)点开始以1,3,9,27,....的步数走的 其实可以每走一步后,以机器人为中心,平面所有坐标全部缩小3倍 那么本应该走3步的路现在只需要走1步就可以到达那个点 那么对于机器人来说这 ...

  10. 使用Euclid算法求最大公约数

    参考文章 1.<linux c编程一站式学习>的习题5.3.1 2.百度百科Euclid算法:https://baike.baidu.com/item/Euclid%E7%AE%97%E6 ...