Count and Say

问题:

Count and Say

解决思路:从1到n,按照规则循环读取即可

Python代码:

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s = "1"
        i = 1
        while i < n:
            s = self.read(s)
            i += 1
        return s
        
    def read(self,s):
        count = 0
        out_s = ""
        for i in range(len(s)):
            if i == 0:
                count += 1
            elif i > 0 and s[i] == s[i-1]:
                count += 1
            else:
                out_s += str(count)+s[i-1]
                count = 1
        return out_s + str(count) + s[i]