My clear python solution


  • 0
    G
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        def generateNext(s):
            curr, next = 0, 1
            length = len(s)
            s = s + '#'
            rv = ''
            while curr < length and next < length+1:
                if s[curr] != s[next]:
                    rv = rv + str(next-curr) + s[curr]
                    curr,next = next, next+1
                else:
                    next += 1
            return rv
        
        s = '1'
        for _ in range(1,n):
            s = generateNext(s)
        return s

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.