{"id":752,"date":"2020-09-26T11:18:32","date_gmt":"2020-09-26T03:18:32","guid":{"rendered":"http:\/\/39.96.58.60\/?p=752"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-6-z-%e5%ad%97%e5%bd%a2%e5%8f%98%e6%8d%a2","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=752","title":{"rendered":"Leetcode 6. Z \u5b57\u5f62\u53d8\u6362"},"content":{"rendered":"<p>\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u4e32\u6839\u636e\u7ed9\u5b9a\u7684\u884c\u6570\uff0c\u4ee5\u4ece\u4e0a\u5f80\u4e0b\u3001\u4ece\u5de6\u5230\u53f3\u8fdb\u884c&nbsp;Z \u5b57\u5f62\u6392\u5217\u3002<\/p>\n<p>\u6bd4\u5982\u8f93\u5165\u5b57\u7b26\u4e32\u4e3a <code>\"LEETCODEISHIRING\"<\/code>&nbsp;\u884c\u6570\u4e3a 3 \u65f6\uff0c\u6392\u5217\u5982\u4e0b\uff1a<\/p>\n<pre>L   C   I   R\n\nE T O E S I I G\n\nE   D   H   N\n\n<\/pre>\n<p>\u4e4b\u540e\uff0c\u4f60\u7684\u8f93\u51fa\u9700\u8981\u4ece\u5de6\u5f80\u53f3\u9010\u884c\u8bfb\u53d6\uff0c\u4ea7\u751f\u51fa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\uff1a<code>\"LCIRETOESIIGEDHN\"<\/code>\u3002<\/p>\n<p>\u8bf7\u4f60\u5b9e\u73b0\u8fd9\u4e2a\u5c06\u5b57\u7b26\u4e32\u8fdb\u884c\u6307\u5b9a\u884c\u6570\u53d8\u6362\u7684\u51fd\u6570\uff1a<\/p>\n<pre>string convert(string s, int numRows);<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> s = \"LEETCODEISHIRING\", numRows = 3\n\n<strong>\u8f93\u51fa:<\/strong> \"LCIRETOESIIGEDHN\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> s = \"LEETCODEISHIRING\", numRows =&nbsp;4\n\n<strong>\u8f93\u51fa:<\/strong>&nbsp;\"LDREOEIIECIHNTSG\"\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\n\nL     D     R\n\nE   O E   I I\n\nE C   I H   N\n\nT     S     G<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a56 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8696.88% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.00% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u6a21\u62dfZ\u5b57\u5f62\u6392\u5217\u8fc7\u7a0b\n    \u521d\u59cb\u5316numRows\u4e2a\u5b57\u7b26\u4e32\uff0c\u5206\u522b\u7528\u505a\u6a21\u62dfz\u81ea\u884c\u6392\u5217\u7684\u5404\u884c\n    \u4f7f\u7528i\u4f5c\u4e3a\u884c\u6570\u7684\u7d22\u5f15\uff0cd\u505a\u4e3a\u7ffb\u8f6c\n    \u6700\u7ec8\u5c06\u5404\u884c\u6570\u636e\u8fde\u63a5\u5728\u4e00\u8d77\n\"\"\"\nclass Solution:\n    def convert(self, s: str, numRows: int) -&gt; str:\n        if numRows &lt;= 1:\n            return s\n\n        record = [\"\" for _ in range(numRows)]\n        i, d = 0, -1\n        for c in s:\n            if i ==0 or i ==numRows-1:\n                d = -d\n            record[i] += c\n            i += d\n        return \"\".join(record)\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a68 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8670.05% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.00% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u603b\u7ed3\u89c4\u5f8b\uff0c\n        \u603b\u4f536\u884c\u65f6\uff0c\u6bcf\u884c\u7684\u6570\u95f4\u9694\u4e3a(10, 0), (8, 2), (6, 4), (4, 6), (2, 8), (0, 10)\n        \u603b\u4f535\u884c\u65f6\uff0c\u6bcf\u884c\u7684\u6570\u95f4\u9694\u4e3a(8, 0), (6, 2), (4, 4), (2, 6), (0, 8)\n        \u603b\u4f534\u884c\u65f6\uff0c\u6bcf\u884c\u7684\u6570\u95f4\u9694\u4e3a(6, 0), (4, 2), (2, 4), (0, 6)\n\n\"\"\"\n\nclass Solution:\n    def convert(self, s: str, numRows: int) -&gt; str:\n        if numRows &lt; 2:\n            return s\n        len_s = len(s)\n        string = \"\"\n        for i in range(numRows):\n            p, q  = numRows*2-2-i*2, i*2\n            j = i\n            overturn = False\n            while j &lt; len_s:\n                step = q if overturn else p\n                if step != 0:\n                    string += s[j]\n                j += step\n                overturn = not overturn\n        return string<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u4e32\u6839\u636e\u7ed9\u5b9a\u7684\u884c\u6570\uff0c\u4ee5\u4ece\u4e0a\u5f80\u4e0b\u3001\u4ece\u5de6\u5230\u53f3\u8fdb\u884c&nbsp;Z \u5b57\u5f62\u6392\u5217\u3002 \u6bd4\u5982\u8f93\u5165\u5b57\u7b26\u4e32\u4e3a &#8220;LEE&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/752"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=752"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":753,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions\/753"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}