Share my straightforward python question


  • 0
    5
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
            cur_s = '1'
            for i in xrange(2, n+1):
                cur_s = self.next_num(cur_s)
            return cur_s
            
        def next_num(self, cur_s):
            new_str = ''
            cur_c = cur_s[0]
            count = 1
            for c in cur_s[1:]:
                if cur_c == c:
                    count += 1
                else:
                    new_str += str(count) + cur_c
                    cur_c = c # reset
                    count = 1
            new_str += str(count) + cur_c
            return new_str

Log in to reply
 

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