38.报数

class Solution:
    def countAndSay(self, n: int) -> str:
        seq = "1"
        for i in range(n-1):
            seq = self.getNext(seq)
        return seq
        
    def getNext(self, seq):
        i, next_seq = 0, ''
        while i < len(seq):
            count = 1
            while i+1 <len(seq) and seq[i] == seq[i+1]:
                count += 1
                i += 1
            next_seq += str(count) + seq[i]
            i += 1
        return next_seq

 

posted @ 2019-08-23 18:29  我叫郑小白  阅读(94)  评论(0编辑  收藏  举报