[LeetCode]Count and Say 计数和发言
Count and Say 计数和发言
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n < 2:
return '1'
ret_str = '1'
while n > 1:
temp, current = '', 0
for i, v in enumerate(ret_str):
if i > 0 and v != ret_str[i-1]:
temp += str(current) + ret_str[i-1]
current = 1
else:
current += 1
ret_str = temp + (str(current) + ret_str[-1] if current > 0 else '')
n -= 1
return ret_str

关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法