python easy ac solution


  • 0
    H
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            s = '1'
            for i in range(n - 1):
                s = self.sayIt(s)
            return s 
        
        def sayIt(self, s):
            res = []
            i = 0
            while i < len(s):
                count = 1
                num = s[i]
                while i + 1< len(s) and s[i] == s[i + 1]:
                    i += 1
                    count += 1
                i += 1
                res += [str(count), str(num)]
            return ''.join(res)
            
    

Log in to reply
 

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