{"id":1704,"date":"2022-01-09T18:54:52","date_gmt":"2022-01-09T10:54:52","guid":{"rendered":"http:\/\/139.9.1.231\/?p=1704"},"modified":"2022-01-09T18:54:54","modified_gmt":"2022-01-09T10:54:54","slug":"leetcodeday6","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/09\/leetcodeday6\/","title":{"rendered":"leetcodeday6 &#8211;Z \u5b57\u5f62\u53d8\u6362"},"content":{"rendered":"\n<p>\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u4e32 s \u6839\u636e\u7ed9\u5b9a\u7684\u884c\u6570 numRows \uff0c\u4ee5\u4ece\u4e0a\u5f80\u4e0b\u3001\u4ece\u5de6\u5230\u53f3\u8fdb\u884c&nbsp;Z \u5b57\u5f62\u6392\u5217\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u8f93\u5165\u5b57\u7b26\u4e32\u4e3a &#8220;PAYPALISHIRING&#8221;&nbsp;\u884c\u6570\u4e3a 3 \u65f6\uff0c\u6392\u5217\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>P A H N<br>A P L S I I G<br>Y I R<br>\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&#8221;PAHNAPLSIIGYIR&#8221;\u3002<\/p>\n\n\n\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\n\n\n<p>string convert(string s, int numRows);<br>&nbsp;<\/p>\n\n\n\n<p>\u793a\u4f8b 1\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1as = &#8220;PAYPALISHIRING&#8221;, numRows = 3<br>\u8f93\u51fa\uff1a&#8221;PAHNAPLSIIGYIR&#8221;<\/p>\n\n\n\n<p><br>\u793a\u4f8b 2\uff1a<br>\u8f93\u5165\uff1as = &#8220;PAYPALISHIRING&#8221;, numRows = 4<br>\u8f93\u51fa\uff1a&#8221;PINALSIGYAHRPI&#8221;<br>\u89e3\u91ca\uff1a<br>P         I        N<br>A     L S    I  G<br>Y A    H R<br>P         I<br>\u793a\u4f8b 3\uff1a<\/p>\n\n\n\n<p>\u8f93\u5165\uff1as = &#8220;A&#8221;, numRows = 1<br>\u8f93\u51fa\uff1a&#8221;A&#8221;<br>&nbsp;<\/p>\n\n\n\n<p>\u63d0\u793a\uff1a<\/p>\n\n\n\n<p>1 &lt;= s.length &lt;= 1000<br>s \u7531\u82f1\u6587\u5b57\u6bcd\uff08\u5c0f\u5199\u548c\u5927\u5199\uff09\u3001&#8217;,&#8217; \u548c &#8216;.&#8217; \u7ec4\u6210<br>1 &lt;= numRows &lt;= 1000<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b21\u5c1d\u8bd5\uff1a\u5206\u522b\u6c42\u5947\u6570\u548c\u5076\u6570\u5217\u7684\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u4e0b\u6807<\/p>\n\n\n\n<p>\u5047\u8bbe\uff1anumrows=5\uff0c\u5bf9\u5e94\u4f4d\u7f6e\u5982\u679c\u6ca1\u6709\u5143\u7d20\uff0c\u8bbe\u4e3a-1<\/p>\n\n\n\n<p>$$\\begin{bmatrix} 0 &amp; -1 &amp;8 &amp; -1 &amp; 16 \\\\ 1 &amp; 7 &amp; 9 &amp; 15 &amp; 17  \\\\ 2 &amp; 6 &amp; 10 &amp; 14 &amp; 18  \\\\ 3 &amp; 5 &amp; 11 &amp; 13 &amp; 19  \\\\ 4 &amp; -1 &amp; 12 &amp; -1 &amp; 20   \\end{bmatrix}$$<\/p>\n\n\n\n<p>\u56e0\u4e3a\u4e00\u822c\u662f\u5bf9\u884c\u64cd\u4f5c\uff0c\u6240\u4ee5\u5148\u5c06\u5176\u8f6c\u7f6e\uff1a<\/p>\n\n\n\n<p> $$\\begin{bmatrix} 0 &amp; 1 &amp;2 &amp; 3 &amp; 4 \\\\ -1 &amp; 7 &amp; 6 &amp; 5 &amp; -1  \\\\ 8 &amp; 9 &amp; 10 &amp; 11 &amp; 12  \\\\ -1 &amp; 15 &amp; 14 &amp; 13 &amp; -1  \\\\ 16&amp; 17 &amp; 18 &amp; 19 &amp; 20   \\end{bmatrix}$$ <\/p>\n\n\n\n<p>\u5148\u6c42\u5076\u6570\u884c\uff0c\u56e0\u4e3a\u6bcf\u884c\u9996\u5143\u7d20\uff1a\u503c\u4e3a nextstart+(numRows-1)*2 \uff0c\u56e0\u6b64\u6bcf\u4e00\u884c\u7684\u503c\u4e3aList[i]=[j for j in range(nextstart,nextstart+numRows)] <\/p>\n\n\n\n<p>\u5947\u6570\u884c\uff1a\u4e3a\u4e0a\u4e00\u884c\u7684\u5c3e\u5143\u7d20\u7684\u987a\u5ef6\uff0c\u5947\u6570\u884c\u9996\u5c3e\u5143\u7d20\u4e3a-1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&#91;i]&#91;0]=-1\nList&#91;i]&#91;numRows-1]=-1\nList&#91;i]&#91;1:numRows-1]=list(range(List&#91;i-1]&#91;numRows-1]+1,List&#91;i-1]<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u8f6c\u7f6e\u56de\u6765\uff0c\u5c06\u4e8c\u7ef4\u8f6c\u4e00\u7ef4\uff0c\u7136\u540e\u53bb\u6389\u6570\u7ec4\u4e2d\u7684-1\u548c\u503c\u5927\u4e8e\u5e8f\u5217\u957f\u5ea6\u7684\u4f4d\u7f6e\uff1a<\/p>\n\n\n\n<p> List=[i for item in List for i in item if i!=-1 and i&lt;length] <\/p>\n\n\n\n<p>\u6700\u540e\u6839\u636e\u4e00\u7ef4\u6570\u7ec4\u5bf9\u5e94\u4f4d\u7f6e\u7684\u503c\u5c31\u662f\u65b0\u5b57\u7b26\u4e32\u5bf9\u5e94\u4e0b\u6807\u7684\u5b57\u7b26<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for j in range(length):\n            string&#91;j]=s&#91;List&#91;j]]\n        return \"\".join(string) \n<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2dnumrows=1\u7684 \u60c5\u51b5\u8981\u5355\u72ec\u8003\u8651<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc code=start\r\nclass Solution:\r\n    def convert(self, s: str, numRows: int) -> str:\r\n        length=len(s)\r\n        if length&lt;=numRows or numRows==1 :\r\n            return s\r\n\r\n        row=(length\/\/((numRows-1)*2)+1)*2\r\n        List=&#91;&#91;-1 for m in range(numRows)] for l in range(row)]\r\n        nextstart=0\r\n    #\u7b2ci\u884c\r\n        for i in range(row):\r\n#\u5982\u679c\u662f\u5076\u6570\u884c\uff1a\r\n\r\n            if(i % 2) == 0:\r\n                List&#91;i]=&#91;j for j in range(nextstart,nextstart+numRows)]\r\n                nextstart=nextstart+(numRows-1)*2\r\n            else:\r\n                List&#91;i]&#91;0]=-1\r\n                List&#91;i]&#91;numRows-1]=-1\r\n                List&#91;i]&#91;1:numRows-1]=list(range(List&#91;i-1]&#91;numRows-1]+1,List&#91;i-1]&#91;numRows-1]+1+numRows-2))&#91;::-1]\r\n        List=list(list(i) for i in zip(*List))\r\n        List=&#91;i for item in List for i in item if i!=-1 and i&lt;length]\r\n        string=list(range(length))\r\n        for j in range(length):\r\n            string&#91;j]=s&#91;List&#91;j]]\r\n        return \"\".join(string) \r\n            <\/code><\/pre>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-162.png\" alt=\"\" class=\"wp-image-1715\" width=\"525\" height=\"138\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-162.png 693w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-162-300x79.png 300w\" sizes=\"(max-width: 525px) 100vw, 525px\" \/><\/figure><\/div>\n\n\n\n<p>\u6539\u8fdb\uff1a<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a\u6309\u884c\u6392\u5e8f<br>\u601d\u8def<\/p>\n\n\n\n<p>\u901a\u8fc7\u4ece\u5de6\u5411\u53f3\u8fed\u4ee3\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u786e\u5b9a\u5b57\u7b26\u4f4d\u4e8e Z \u5b57\u5f62\u56fe\u6848\u4e2d\u7684\u54ea\u4e00\u884c\u3002<\/p>\n\n\n\n<p>\u7b97\u6cd5<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 \\(\\text{min}( \\text{numRows}, \\text{len}(s))\\) \u4e2a\u5217\u8868\u6765\u8868\u793a Z \u5b57\u5f62\u56fe\u6848\u4e2d\u7684\u975e\u7a7a\u884c\u3002<\/p>\n\n\n\n<p>\u4ece\u5de6\u5230\u53f3\u8fed\u4ee3 s\uff0c\u5c06\u6bcf\u4e2a\u5b57\u7b26\u6dfb\u52a0\u5230\u5408\u9002\u7684\u884c\u3002\u53ef\u4ee5\u4f7f\u7528\u5f53\u524d\u884c\u548c\u5f53\u524d\u65b9\u5411\u8fd9\u4e24\u4e2a\u53d8\u91cf\u5bf9\u5408\u9002\u7684\u884c\u8fdb\u884c\u8ddf\u8e2a\u3002<\/p>\n\n\n\n<p>\u53ea\u6709\u5f53\u6211\u4eec\u5411\u4e0a\u79fb\u52a8\u5230\u6700\u4e0a\u9762\u7684\u884c\u6216\u5411\u4e0b\u79fb\u52a8\u5230\u6700\u4e0b\u9762\u7684\u884c\u65f6\uff0c\u5f53\u524d\u65b9\u5411\u624d\u4f1a\u53d1\u751f\u6539\u53d8\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"917\" height=\"363\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-163.png\" alt=\"\" class=\"wp-image-1718\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-163.png 917w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-163-300x119.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-163-768x304.png 768w\" sizes=\"(max-width: 917px) 100vw, 917px\" \/><\/figure>\n\n\n\n<p>\u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09<br>\u94fe\u63a5\uff1ahttps:\/\/leetcode-cn.com\/problems\/zigzag-conversion<br>\u8457\u4f5c\u6743\u5f52\u9886\u6263\u7f51\u7edc\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u5b98\u65b9\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u4e32 s \u6839\u636e\u7ed9\u5b9a\u7684\u884c\u6570 numRows \uff0c\u4ee5\u4ece\u4e0a\u5f80\u4e0b\u3001\u4ece\u5de6\u5230\u53f3\u8fdb\u884c&nbsp;Z \u5b57\u5f62\u6392\u5217\u3002 \u6bd4 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/09\/leetcodeday6\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday6 &#8211;Z \u5b57\u5f62\u53d8\u6362<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1704"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=1704"}],"version-history":[{"count":13,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1704\/revisions"}],"predecessor-version":[{"id":1719,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/1704\/revisions\/1719"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=1704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=1704"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=1704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}