{"id":2877,"date":"2020-02-04T12:32:47","date_gmt":"2020-02-04T04:32:47","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2877"},"modified":"2020-02-04T12:32:47","modified_gmt":"2020-02-04T04:32:47","slug":"%e3%80%90leetcode%e3%80%910038-%e5%a4%96%e8%a7%82%e6%95%b0%e5%88%97","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2877\/","title":{"rendered":"\u3010LeetCode\u30110038 \u5916\u89c2\u6570\u5217"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/count-and-say\/\">https:\/\/leetcode-cn.com\/problems\/count-and-say\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def countAndSay(self, n):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> n: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: str\n        \"\"\"\n        <\/em>if n == 1:\n            return '1'\n        pre_str = self.countAndSay(n - 1)\n        res = ''\n        count = 1\n        temp_list = []\n        for i in range(len(pre_str) - 1):\n            if pre_str[i] == pre_str[i + 1]:\n                count += 1\n            else:\n                temp_list.append(str(count) + pre_str[i])\n                count = 1\n        if count == 1:\n            temp_list.append('1' + pre_str[-1])\n        else:\n            temp_list.append(str(count) + pre_str[-1])\n        for i in range(len(temp_list)):\n            res += temp_list[i]\n        return res\nif __name__ == '__main__':\n    n = 5\n    print(Solution().countAndSay(n))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u5c31\u611f\u89c9\u8fd9\u4e2a\u7b80\u5355\u4e2d\u7b49\u96be\u9898\u6709\u65f6\u5019\u5c31\u4e0d\u592a\u61c2\uff0c\u6709\u7684\u7b80\u5355\u9898\u5427\uff0c\u597d\u50cf\u4e5f\u4e0d\u5c5e\u4e8e\u7b80\u5355\uff0c\u6709\u7684\u96be\u9898\u597d\u50cf\u4e5f\u4e0d\u96be\uff0c\u5bb3\u3002<\/p>\n\n\n<p>\u8fd9\u9053\u9898\u9012\u5f52\uff0c\u6bcf\u6b21\u5904\u7406\u4e0a\u4e00\u6b21\u7684\u7ed3\u679c\uff0c\u53d6\u4e00\u4e2a\u4e2d\u95f4\u5217\u8868\uff0c\u8bb0\u5f55\u904d\u5386\u7684\u7ed3\u679c\uff0c\u5982\u679c\u5f53\u524d\u7d22\u5f15\u548c\u4e0b\u4e00\u4e2a\u4e00\u6837\uff0c\u5219\u6807\u8bb0+1\uff0c\u4e0d\u4e00\u6837\u5c31\u5728\u5217\u8868\u4e2d\u653e\u5165\u2018\u6807\u8bb0+\u5b57\u7b26\u2019\u3002<\/p>\n\n\n<p>\u6700\u540e\u518d\u5904\u7406\u4e00\u4e0b\u6700\u540e\u4e00\u4f4d\uff0c\u56e0\u4e3a\u8981\u548c\u4e0b\u4e00\u4f4d\u6bd4\u8f83\uff0c\u6240\u4ee5\u5faa\u73af\u6bd4\u957f\u5ea6\u5c11\u4e00\u4f4d\uff0c\u56e0\u6b64\u6700\u540e\u5904\u7406\u8be5\u4f4d\u3002\u5206n\u4e2a\u91cd\u590d\u548c1\u4e2a\u72ec\u7acb\u4e24\u79cd\u60c5\u51b5\uff0c\u56e0\u6b64\u5355\u72ec\u5224\u65ad\u5904\u7406\uff0c\u6700\u540e\u5c06\u5217\u8868\u8f6c\u4e3astr\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/count-and-say\/ #  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1689,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2877"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2877"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2877\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2877"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}