leetcode:Count and Say【Python版】

时间:2023-03-09 08:27:08
leetcode:Count and Say【Python版】

一次AC

字符串就是:count+char

 class Solution:
# @return a string
def countAndSay(self, n):
str = ""
for i in range(n-1):
tmp = str
str = ""
c = tmp[0]
cnt = 1
for j in range(1,len(tmp)):
if tmp[j] == tmp[j-1]:
cnt += 1
else:
str += ("%d"%cnt + tmp[j-1])
cnt = 1
str += ("%d"%cnt + tmp[len(tmp)-1])
return str