Straightforward python count and say


  • 0
    W

    My straightforward python count and say
    since we return an string, we can work directly with string.

    def countAndSay(n):
        # my intuitive idea
        if n == 0:
            return []
        seq = '1'
        for i in xrange(1, n):
            ct = 1
            newseq = ''
            last = 0
            for j in range(1, len(seq)):
                if seq[last] == seq[j]:
                    ct += 1
                    last += 1
                else:
                    newseq += str(ct) + seq[last] # see a new number, count and say
                    ct = 1 # reset counter
                    last = j # move pointer 1
            if last == (len(seq) - 1):
                newseq += str(ct) + seq[last]
    
            seq = newseq
        return seq
    

Log in to reply
 

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