Show Answer of Python.


  • 0
    L

    increase the start, and count .

    class Solution:
    # @return a string
    def countAndSay(self, n):
        if(n==1):
            return "1";
        else:
            prev=self.countAndSay(n-1);
            res='';
            
            start=0;
            while(start < len(prev)):
                cur=prev[start];
                count=0;
                while(start < len(prev) and prev[start]==cur): #key point, this will be true for the first time,
                    count+=1;
                    start+=1;                
                res+=str(count)+str(cur);
            return res;

Log in to reply
 

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