Easy python solution with recurdion


  • 0
    N
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            if n<=1:
                return '1'
                
            pre_seq = self.countAndSay(n-1)
            count = 1
            ans = ''
    
            for i in range(len(pre_seq)):
                if i == len(pre_seq)-1 or pre_seq[i]!=pre_seq[i+1]:
                    ans = ans + str(count) + pre_seq[i]
                    count = 1
                else:
                    count += 1
            
            return ans
    

Log in to reply
 

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