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%
近期评论