38. Count and Say
Leetcode link for this question
Discription:
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.
Analyze:
Code 1:
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
def fun(s):
p1,p2=0,0
re=''
for i,v in enumerate(s):
p2=i
if s[p1]==s[p2]:
continue
else:
re=re+str(p2-p1)+s[p1]
p1=p2
re=re+str(p2-p1+1)+s[p1]
return re
s=str(1)
for i in range(n-1):
s=fun(s)
return s
Submission Result:
Status: Accepted
Runtime: 48 ms
Ranking: beats 81.75%
本文介绍了一种生成计数并说序列的方法,通过Python实现,解析了LeetCode上第38题的具体解决方案。该算法接受一个整数n作为输入,并生成对应的计数并说序列。
637

被折叠的 条评论
为什么被折叠?



