Accepted python code


  • 0
    L

    The code is pretty straightforward with the comment. Please comment if you have any questions or suggestions. Thanks!

    class Solution:
        # @return a string
        def countAndSay(self, n):
            # to say the input list
            def say(A):
                jj,res1=1,[1,A[0]]
                while jj<len(A):
                    if A[jj]==res1[-1]:
                        res1[-2]+=1
                    else:
                        res1.append(1)
                        res1.append(A[jj])
                    jj+=1
                return res1
                
            ii,ans,res=1,[1],''
            # say n-1 times
            while ii<n:
                ans,ii = say(ans),ii+1
            # turn the final list into a string
            for num in ans:
                res+=str(num)
            return res

Log in to reply
 

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