Count and Say

Posted wenqinchao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]
        

 

以上是关于Count and Say的主要内容,如果未能解决你的问题,请参考以下文章

5.Leetcode 38:Count and Say 笔记

LeetCode 37. Count and Say

LeetCode38. Count and Say

38. Count and Say - Unsolved

leetcode 38 Count and Say

38. Count and Say