leetcode 链接:https://leetcode-cn.com/problems/count-and-say/
python: 20ms, 10.8mb, 97%
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if n == 1: return "1"
else:
return self.retRes(self.countAndSay(n - 1))
def retRes(self, str_):
pre = res = cnt = ""
for i in range(len(str_)):
if pre != str_[i]:
res = res + str(cnt) + pre
pre = str_[i]
cnt = 1
else: cnt += 1
res = res + str(cnt) + pre
return res
其他方法看 leetcode 链接 评论区~