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ㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
随机推荐
- LCIS HDU - 3308 (线段树区间合并)
LCIS HDU - 3308 Given n integers. You have two operations: U A B: replace the Ath number by B. (inde ...
- tensorflow 分布式训练
TF实现分布式流程 1.创建集群 ClusterSpec & Server cluster = tf.train.ClusterSpec({"ps": ps_hosts, ...
- MySQL--主备相关命令
创建用户账号 GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO repl@'10.70.8.%' IDENTIFIED BY 'mysql'; ...
- shell中通过sed替换文件中路径
通常sed指令修改行内容时使用:sed -i " 9 s/^.*/"type in what you want modified!"/" 其中"typ ...
- gradle配置多个代码仓库repositories
repositories { mavenCentral() maven { url "https://jitpack.io" } maven { url "http:// ...
- JAVA初学者——标识符命名规则及数据类型的转换
Hello!我是浩宇大熊猫~ 直接进入正题吧~ 1)标识符的命名规则. 标识符命名法有小驼峰命名法和大驼峰命名法两种,分别应用于方法.变量和类. 小驼峰命名法应用于方法和变量,主要有两个约定: 1.标 ...
- 17。3.12---re模块--正则表达式操作指南
1----python re模块(Regular Expressioin正则表达式)提供了一个与perl等编程语言类似的正则匹配操作,他是一个处理python字符串的强有力的工具,有自己的语法和独立的 ...
- lua 把图片转换成base64
调用实例 require("ZZBase64") local files local file = io.open("E:\\2342.jpg","r ...
- Ubuntu下查看硬盘分区UUID的方法&所有Linux目录樹
在Ubuntu中UUID的两种获取方法,至于UUID是什么,你可以大概理解为分区的标识符,像条形码那样. 在终端中输入下面的命令就可心查看到分区UUID了.命令1.sudo blkid 命令2.ls ...
- 使用pythonnet调用halcon脚本
最近的项目中遇到了使用python程序结合不同部分,其中包括使用halcon处理拍摄到的图像. halcon本身提供了c++与.NET的开发库,但无python库,网上有pyhalcon之类的库,但功 ...