边工作边刷题:70天一遍leetcode: day 34-1

Count and Say

要点:考点就是count和duplicate的code pattern:如果基本条件判断nums[i]和nums[i+1],那么要loop从0到n-1,但最后一个元素特殊处理:这种情况和不相等同样处理。如果是判断nums[i]和nums[i-1],那么要loop从1到n,最后特殊处理。
错误点:

  • next要append
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        pre = "1"
        for i in range(2, n+1):
            count=1
            next = []
            for j in range(len(pre)):
                if j==len(pre)-1 or pre[j]!=pre[j+1]:
                    next.append(str(count))
                    next.append(pre[j])
                    count = 1
                else:
                    count+=1
            pre=''.join(next)
        return pre
                    
原文地址:https://www.cnblogs.com/absolute/p/5678193.html