<LeetCode><Easy> 38 Count and Say

来源:互联网 发布:mac 10.12.6系统怎么样 编辑:程序博客网 时间:2024/06/15 18:10

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

#Python2  68ms

class Solution(object):    def countAndSay(self, n):        """        :type n: int        :rtype: str        """        def H(x):            rList,count,tmp="",0,x[0]            for i in range(len(x)):                if x[i] == tmp:                    count+=1                else:                    rList,tmp,count=rList+str(count)+tmp,x[i],1            return rList+str(count)+tmp        x='1'        for i in range(n-1):            x=H(x)        return x


0 0
原创粉丝点击