Python solution beats 96%


  • 0
    A

    Nothing special

    '''
    The count-and-say sequence is the sequence of integers beginning as follows:
    1, 11, 21, 1211, 111221, ...
    
    1 is read off as "one 1" or 11.
    11 is read off as "two 1s" or 21.
    21 is read off as "one 2, then one 1" or 1211.
    Given an integer n, generate the nth sequence.
    '''
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            s="1"
            tmp=""
            for i in range(n-1):
                t=s[0]
                c=0
                for j in range(len(s)):
                    if s[j]!=t:
                        tmp+=str(c)
                        tmp+=t
                        c=1
                        t=s[j]
                    else:
                        c+=1
                tmp+=str(c)
                tmp+=t
                s=tmp
                tmp=""
            return s
    

Log in to reply
 

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