Python, LintCode, 420. 报数

class Solution:
    """
    @param n: the nth
    @return: the nth sequence
    """
    def countAndSay(self, n):
        # write your code here
        if n == 1:
            return "1"
        if n == 2:
            return "11"
        pre = self.countAndSay(n-1)
        res = ""
        count = 1
        for i in range(len(pre)-1):
            tmp = pre[i]
            next = pre[i+1]
            if tmp == next:
                count += 1
            else:
                res = res + str(count) + str(tmp)
                count = 1
        res = res + str(count) + str(next)
        return res

猜你喜欢

转载自blog.csdn.net/u010342040/article/details/80287089
今日推荐