Python solution


  • 0
    G
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            ans = {1: '1'}
            for i in range(2, n+1):
                prev = ans[i-1][0]
                cnt = 1
                result = ''
                for digit in ans[i-1][1:]:
                    if digit == prev:
                        cnt += 1
                    else:
                        result = result+str(cnt)+prev
                        prev = digit
                        cnt = 1
                ans[i] = result+str(cnt)+prev
            return ans[n]
    

Log in to reply
 

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