38. Count and Say [easy] (Python)

大模型之Spring AI实战系列(二十一):Spring AI + MCP + 本地文件系统实战指南 在上一篇文章中,我们学习了如何使用 Spring AI 集成 Model Context Protocol (MCP),并通过 Brave Search 实现了互联网搜索能力的接入。本文将继续深入 Spring AI 的 MCP 实战能力,介绍如何将 **本地文件系统** 封装为 LLM 可调用的工具 阅读详情

题目链接

https://leetcode.com/problems/count-and-say/

题目原文

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

题目翻译

一个”数和说(count and say)“序列是像这样的一个整数序列:1, 11, 21, 1211, 111221, ...。
1 读作 "1个1" 或 11.
11 读作 "2个1" 或 21.
21 读作 "1个2,1个1" 或 1211.
给定整数n,生成第n个序列。
注意:整数序列用字符串表示。

思路方法

这个问题重点首先是要读懂题目。然后在想这个过程的时候,你可能会想的比较复杂,比如,会不会出现连续十几个一样的数字,比如1111111111,读作“ten 1”,然后转换成101。实际上,有大神已经证明,这个序列里的数不会超过4。这边搬运了一下。
原链接:https://leetcode.com/discuss/6762/how-to-proof-the-count-is-always-less-than-10

Proof by exhaustion and contrapositive:

In order for a number greater than 4 to be created, there must be a series of numbers n>4 in length all the same digit. Therefore, there is a subset of that series where the count would only reach 4. Because of this, any proof for the existence of a chain resulting in a number greater than 4 is also a proof for the existence of a 4-chain. Using the proof by contrapositive, this means that if 4-chains are proved to be impossible, then any n-chain with n>4 is also impossible.

In order to start with a chain with numbers greater than 4, you must assume that a 4-chain is possible in the first place, which is circular reasoning, and so cannot be used as an initial point. It is further impossible to have a negative value, since the counting numbers do not include them.

Therefore, the only chains able to create a 4 (at least the first one) are 0000, 1111, 2222, or 3333.

0 0 0 0 -> 40
The 0000 is read zero 0, zero 0, which must come from . Since there is nothing present, it could in theory occur anywhere in the string. However, since they would be next to each other, if the 0 is repeated as would be neccessary, the zeros would add together, resulting in just zero 0, leaving only 20, not 40.

1 1 1 1 -> 41
The 1111 is read one 1, one 1 (or 11), which translates to 21, not 1111. This contradicts the assumption that there is a way to get 1111, and so prevents 4 or greater from appearing. Therefore, 1s cannot reach 4.

2 2 2 2 -> 42
The 2222 is read two 2, two 2 (or 22 22), which is identical to the output. Since the input maps to itself, there is no way to leave that cycle, or it already would have. If 2222 exists in the input, then 2222 must have mapped to it. It cannot reach 42. Therefore, 2s cannot reach 4.

3 3 3 3 -> 43
The 3333 is read three 3, three 3 (or 333 333). This in turn would require 333 333 333. This fails in two respects. First, that the previous inputs would not merge to 63 or 93. The second, that the sequence eventually traces back to the origin, 1. Since it keeps increasing in length as the number of rounds since the start decreases, it cannot have started at 1. Therefore, 3s cannot reach 4.

As every possible case has been examined, and none can reach a 4 while starting at the given beginning (1), it is not possible for a 4-chain to occur, meaning a 4 cannot appear in any valid string for this problem. Further, as stated above, since a 4-chain is impossible, so too are all n-chains with n>4, so no number greater than 4 can appear either.

思路一

根据题目描述的过程写代码,完全遵照规则写就行了。所以要用到三个循环,最外层是生成第几个序列;中间层用于遍历一个序列;最内层找出连续相同的几个数字。

代码

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        res = '1'
        for i in xrange(n-1):
            new_res = ''
            j = 0
            while j < len(res):
                count = 1
                while j < len(res)-1 and res[j] == res[j+1]:
                    j = j + 1
                    count = count + 1
                j = j + 1
                new_res = new_res + str(count) + res[j-1]
            res = new_res
        return res

思路二

当然,其实最内层和中间层不需要分开写,用一个pre变量存放上一个字符即可。

代码

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        res = '1'
        for i in xrange(n-1):
            new_res, pre, count = '', res[0], 0
            for j in xrange(len(res)):
                if res[j] == pre:
                    count += 1
                else:
                    new_res += str(count) + pre
                    count = 1
                    pre = res[j]
            res = new_res + str(count) + pre
        return res

思路三

其实Python有个itertools.groupby()可以帮助我们数连着的数字,所以代码可以精简很多。

代码

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        res = '1'
        for i in xrange(n-1):
            res = ''.join([str(len(list(group))) + digit for digit, group in itertools.groupby(res)])
        return res

PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.youkuaiyun.com/coder_orz/article/details/51702322

外部PLC触发VisionMaster多流程运行 上一讲,介绍了海康VisionMaster与西门子PLC1200进行S7通信。在前一讲的基础上,讲解如何通过PLC触发,实现VisionMaster的多流程运行。 阅读详情

相关推荐

嵌入式状态机编程-QP状态机框架与常见状态机方法

状态机基本术语 现态:是指当前所处的状态。 条件:又称为“事件”,当一个条件被满足,将会触发一个动作,或者执行一次状态的迁移。 动作:条件满足后执行的动作。动作执行完毕后,可以迁移到新的状态,也可以仍旧保持原状态。动作不是必需的,当条件满足后,也可以不执行任何动作,直接迁移到新状态。 次态:条件满足后要迁往的新状态。“次态”是相对于“现态”而言的,“次态”一旦被激活,就转变成新的“现态”了。 传统有限状态机Fsm实现方法 如图,是一个定时计数器,计数器存在两种状态,一种为设置状态,一种为计时状态 设

qq_36969440的博客 9663

Count and Say @python 题解

Count and Say python @题解

随便记点东西 2586

绕过限制?实测修改UA在PC端运行微信小程序的可行性及风险

本文探讨了通过修改User-Agent(UA)在PC端运行微信小程序的技术可行性及风险。分析了小程序设备限制的技术原理,包括`wx.getSystemInfo`API和UA检测机制,并提供了多种修改方案及其局限性。同时强调了潜在的安全和法律风险,推荐使用官方模拟器或安卓模拟器等更安全的替代方案。

weixin_30879169的博客 362

python算法-1字符串-10数数Leetcode 038 Count and Say

python算法-1字符串-10数数Leetcode 038 Count and Say 在线运行:https://pyleetcode.gitee.io/codes_html/Leetcode_038_Count_and_Say.html def countAndSay(n): if n == 0: return '' res = '3' while n != 0: n -= 1 i = 0 cou..

千寻的博客 340

小白学电路之cadance搭建buffer电路

Buffer(缓冲器)是一种放大器或驱动电路,主要用于隔离输入和输出,以防止负载对前级电路造成干扰或影响。本文作者在电路方面是小白水平,目前正在学习,有不足之处欢迎指正。3.创建新的cellview,对此buffer进行仿真验证。2.创建buffer的symbol。电路中的V2的详细参数。

weixin_58051657的博客 1984

距离多普勒谱(RD谱)绘制方法及理解

RD谱是信号处理领域常用的图形。原始信号往往是一维时域序列,如何将其二维化,并进行变换生成RD谱呢?由于刚刚接触,查阅了网络上、老师手里的资料,发现RD谱的绘制方法并不统一,有些存在一定问题。在此,将心得体会记录下来。

mddh_123的博客 3万+

Count-And-Say——python

“”” 报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 被读作 “one 1” (“一个一”) , 即 11。 11 被读作 “two 1s” (“两个一”), 即 21。 21 被读作 “one 2”, “one 1” (”

The Road Of Sec 417

秦时明月6.2魔改版_从零到一部署指南_含安卓客户端配置与GM后台直通

本文提供《秦时明月》6.2魔改版从零开始的完整部署指南。详细讲解CentOS服务器环境准备、服务端文件部署与安装、关键端口开放及进程验证。重点涵盖安卓客户端APK的IP修改与重签名方法,并介绍了GM后台工具的访问与使用,助你快速搭建并管理专属游戏服务器。

garlic的博客 310

【LeetCode 38】Count and Say(Python)

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one 1" or 11. 11 i

Lucy_R的博客 1532

告别WSL!用AMD ROCm7在Windows上跑ComfyUI,我的幻x2025 AI Max+ 395体验与性能调优心得

本文详细介绍了在搭载AMD Ryzen AI Max+ 395处理器的幻x2025笔记本上,如何通过ROCm7在Windows原生环境下高效运行ComfyUI,并分享了一系列性能调优技巧和实战经验,帮助开发者摆脱WSL束缚,提升AI创作效率。

weixin_29102619的博客 1115

当C#遇见声音魔法:从麦克风到文字的0.1秒奇幻漂流

为什么选300Hz?因为人声基频通常在85-255Hz,但清脆的“啊”可能到2kHz,所以300Hz是安全区!(参考知识库[1])

墨夶的博客 365

LeetCode 38 Count and Say(C,C++,Java,Python)

Problem: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 i

算法之道 3624

微信小程序 - 实现人脸识别对比+人脸相似度数据,用户从相册选择或拍照上传两张图片进行人脸对比得到人脸相似度及判断是否是同一个人,微信小程序检测人脸相似度(免费纯前端方案+调用第三方API接口方案)

微信小程序实现人脸识别对比,微信小程序人脸对比 人脸对比识别 人脸对比相似度,实现获取手机摄像头权限,调用相机拍照实现人脸识别相似度对比,小程序人脸相似度对比,快速识别两张照片的相似程度(用百分比)图片相似度对比在线测试,图片相似度对比,微信小程序人脸相似度对比分析,计算两张人脸的相似度,从而判断是否同一个人,并给出相似度评分。小程序如何实现人脸相似度比对,小程序纯前端怎么对比两张图片的相似度,计算两幅图像的相似度,图像相似度对比方法,调用百度AI的人脸识别,微信小程序中实现人脸识别认证,实现人脸检测、人脸

🏆 前端领域优质创作者 4862

LeetCode38. Count and Say-python(easy)

题目来源:     https://leetcode.com/problems/count-and-say/description/题目分析:   首先,题目告诉我们count and say是这样的序列:1. 1 2. 11 3. 21 4. 1211 5. 111221它是这样计算的,比如第一个数是1,则第二个数是个数加上数字,也就是一个1,所以是11;...

revivre的博客 322

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

38.leetcode38 Count and Say

本题难度为easy,给了5个字符串,根据规律推测出后续行数的字符,题目要求输入行数返回当行字符。答案里面讲输入行数分成了3类, 1.如果为0返回空字符 2.如果小于等于5直接返回已有字符 3.大于5后开始每行按照规律计算,直到输入行数并返回值 38.Count and Say Easy 12839542Add to ListShare The count-and-say sequence is the sequence of integers with the first five term.

haidixipan的博客 235

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

【leetcode】38. Count and Say

题目如下: 解题思路:本题题目难度是easy,但是题意理解起来的难度是medium。题意大致是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。依次类推,写个countAndSay(n)函数返回字符串。看懂了题意其实就很简单了...

weixin_33958366的博客 88

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

[LeetCode] 038. Count and Say (Easy) (C++/Python)

[LeetCode] 038. Count and Say (Easy) (C++/Python)

水果君の日常 2666

[LeetCode] 38. Count and Say_Easy

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1is read off as"one 1"or11.11is read ...

weixin_30666753的博客 112

LeetCode-38 count and say

题目:The count-and-say sequence is the sequence of integers with the first five terms as following:1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one 1" or 11.11 is read off as...

小零件儿的博客 237

Leetcode||38. Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one 1" or 11. 11 is read

Lingfu74的专栏 238

【LeetCode题意分析&解答】38. Count and Say

The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, the...

weixin_30822451的博客 121

38.leetcode 外观数列(简单)

leetcode python 刷题记录,从易到难 一、题目 二、解答 1.思路 本题思路类似26,27题,都需要找到重复的次数。 根据n开始循环,因为已经定义了1所以需要循环n-1次。 定义快慢双指针指向0,定义临时变量。双指针用来找到重复的次数,临时变量用来存储找到的结果(数字出现次数和数字本身)。 遍历数字字符串,如果快指针找到和慢指针不一样的,慢指针指向的数字重复次数就等于快指针指向的索引减慢指针指向的索引,此时把找到的数字重复次数和数字本身拼接起来存储到零食变量里,然后慢指针指向快指针的位置

终回首的博客 176

LeetCode小白菜笔记[12]:Count and Say

LeetCode小白菜笔记[12]:Count and Say 38. count and say [easy] 题目如下: The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 12...

VisualCortex 580

工作中使用到的单词(软件开发)

1.エイリアス   alias 别名 powershell使用 get-help 命令时,会得到与此命令 相关(别名)的命令 2.プロキシ proxy 代 理 3.プロモート   promote 促进,提升   开发的系统新功能上线发布,有时会使用到这个词,比如 STプロモート 还有使用リリース这个词的。比如STリリース。个人认为两者含义相同。 4.見逃し み................................................................

sun0322 6万+

脑语言2500单字v1.5.1

如果人和 AI 说同一种语言,文明会怎样?我用 2500 个单字试了试。2500个单字是不同领域、不同知识的共同体,无论你学编程或者是数学或写作,你都要用汉字,而常用的2500个汉字几乎可以涵盖不同行业做的不同事情。

脑语言 2002

[leetcode] 38. Count and Say @ python

原题 The count-and-say sequence is the sequence of integers with the first five terms as following: 1 11 21 1211 111221 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21....

闲庭信步 398
上一篇: 58. Length of Last Word [easy] (Python)
下一篇: 190. Reverse Bits [easy] (Python)
coder_orz
博客等级 码龄14年 102粉丝 90原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值