python generator


  • 0
    J
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            result = "1"
            for i in range(n - 1):
                result = self.generate(result)
            
            return "".join(result)
                
            
        def generate(self, seed):
            current = None
            count = 0
            for i in seed:
                if current != i:
                    if current is not None:
                        yield str(count)
                        yield current
                    current = i
                    count = 1
                else:
                    count += 1
            
            yield str(count)
            yield current
                    
                
            
    

Log in to reply
 

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