前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[Leetcode][python]Count and Say/报数

[Leetcode][python]Count and Say/报数

作者头像
蛮三刀酱
发布2019-03-26 16:46:37
5550
发布2019-03-26 16:46:37
举报

题目大意

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.

解题思路

刚开始题意没理解,题意是n=1时输出字符串1;n=2时,数上次字符串中的数值个数,因为上次字符串有1个1,所以输出11;n=3时,由于上次字符是11,有2个1,所以输出21;n=4时,由于上次字符串是21,有1个2和1个1,所以输出1211。

代码

代码语言:javascript
复制
class Solution(object):

    def count(self,s):
        result = '' 
        count = 0
        char = '#'
        for i in s:
            if i!=char:
                if char!='#':
                    result+=str(count)+char
                char = i
                count = 1
            else:
                count+=1
        result += str(count)+char
        return result

    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s='1'
        for i in range(1, n):
            s=self.count(s)
        return s

总结

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年08月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目大意
  • 解题思路
  • 代码
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档