leetcode -- Count and Say -- 理解题意

来源:互联网 发布:x5效果器调试参数数据 编辑:程序博客网 时间:2024/05/27 09:47

https://leetcode.com/problems/count-and-say/

重点在理解题意。count and say

class Solution(object):    def countAndSay(self, n):        """        :type n: int        :rtype: str        """        if n == 1:            return "1"        else:            last = self.countAndSay(n - 1)            digit,count = last[0], 0            res = ""            for i in xrange(len(last)):                if last[i] != digit:                    res += str(count) + str(digit)                    digit = last[i]                    count = 1                else:                    count += 1            return res + str(count) + str(digit)
0 0
原创粉丝点击