美文网首页
LeetCode--报数(python版)

LeetCode--报数(python版)

作者: 猫爱吃草莓 | 来源:发表于2019-01-08 18:56 被阅读0次
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        if n<=0:
            return None
        if n>30:
            return None
        list1=['1','11']
        for i in range(1,n-1):
            a=''
            res=1
            for j in range(1,len(list1[i])):
                if list1[i][j]!=list1[i][j-1]:
                    a=a+str(res)+list1[i][j-1]
                    res=1           # res置1
                else:
                    res=res+1
            a=a+str(res)+list1[i][j]    #对最后一位字符的处理
            list1.append(a)
        return list1[n-1]

重点:

  1. 记录前一个字符结果
  2. 使用两个中间变量res/a
  3. 下标要搞清楚

相关文章

网友评论

      本文标题:LeetCode--报数(python版)

      本文链接:https://www.haomeiwen.com/subject/mybbrqtx.html