12-line Python solution


  • -1
    K
    def countAndSay(self, n):
        if n == 1: return '1'
        s, newS, pre, count = ['1', '#'], [], None, 1
        for i in xrange(n - 1):
            for ch in s:
                if ch != pre:
                    if pre: newS += (str(count), pre)
                    count = 1
                else: 
                    count += 1
                pre = ch
            s, newS, pre, count = newS + ['#'], [], None, 1
        return ''.join(s[:-1])

Log in to reply
 

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