python code beats 81.11% submissions


  • 0
    P
    class Solution(object):
        def fun(self,strn):
            #print 'strn:',strn
            if(len(strn)==1):
                return '11'
            d={}
            lastchar=strn[0]
            count=0
            res=''
            for i in range(len(strn)):
                if(strn[i]==lastchar):
                    count+=1
                else:
                    res+=str(count)+lastchar
                    lastchar=strn[i]
                    count=1
            return res+str(count)+lastchar
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            #print n
            if(n==0):
                return '0'
            res='1'
            count=n-1
            while(count>0):
                res=self.fun(res)
                print 'res:',res
                count-=1
            return res
    

Log in to reply
 

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