❤leetcode,python2❤报数序列是指一个整数序列,按照其中的整数的顺序进行报数,得到下一个数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22795513/article/details/80655700
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        old = '1'
        for i in range(n-1):
            num = 1
            value = ''
            next = ''
            while 1:
                try:
                    if old[0]==old[1]:
                        num += 1
                        value = old[0]
                    else:
                        if value!='':
                            next+= str(num)+str(value)
                        else:
                            next+= str(num)+str(old[0])
                        num = 1
                        value = ''
                    old=old[1:]
                except IndexError,e:
                    next += str(num)+str(old[0])
                    old = next
                    break
        return old

猜你喜欢

转载自blog.csdn.net/qq_22795513/article/details/80655700