首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

数据结构算法操作试题(C++/Python)——报数

1. 题目

leetcode 链接:https://leetcode-cn.com/problems/count-and-say/

2. 解答

python: 20ms, 10.8mb, 97%

代码语言:javascript
复制
class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        if n == 1: return "1"
        else:
            return self.retRes(self.countAndSay(n - 1))
            
    def retRes(self, str_):
        pre = res = cnt = ""
        for i in range(len(str_)):
            if pre != str_[i]:
                res = res + str(cnt) + pre
                pre = str_[i]
                cnt = 1
            else: cnt += 1
        res = res + str(cnt) + pre
        return res

其他方法看 leetcode 链接 评论区~

下一篇
举报
领券