{"id":345,"date":"2022-07-07T15:26:06","date_gmt":"2022-07-07T07:26:06","guid":{"rendered":"http:\/\/nas.hepcloud.top:7000\/?p=345"},"modified":"2022-11-01T22:54:20","modified_gmt":"2022-11-01T14:54:20","slug":"6-z-%e5%ad%97%e5%bd%a2%e5%8f%98%e6%8d%a2","status":"publish","type":"post","link":"http:\/\/www.huerpu.cc:7000\/?p=345","title":{"rendered":"6. Z \u5b57\u5f62\u53d8\u6362"},"content":{"rendered":"<p><a href=\"http:\/\/www.huerpu.cc:7000\">\u5c0f\u666f\u54e5\u54e5\u535a\u5ba2<\/a><\/p>\n<p><a href=\"http:\/\/www.huerpu.cc:2080\">\u4ee3\u7801\u4ed3\u5e93<\/a><\/p>\n<h3>6. Z \u5b57\u5f62\u53d8\u6362<\/h3>\n<p>\u5c06\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u4e32 <code>s<\/code> \u6839\u636e\u7ed9\u5b9a\u7684\u884c\u6570 <code>numRows<\/code> \uff0c\u4ee5\u4ece\u4e0a\u5f80\u4e0b\u3001\u4ece\u5de6\u5230\u53f3\u8fdb\u884c <code>Z<\/code> \u5b57\u5f62\u6392\u5217\u3002<\/p>\n<p>\u6bd4\u5982\u8f93\u5165\u5b57\u7b26\u4e32\u4e3a<code>&quot;PAYPALISHIRING&quot;<\/code> \u884c\u6570\u4e3a <code>3<\/code> \u65f6\uff0c\u6392\u5217\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-shell\">P   A   H   N\nA P L S I I G\nY   I   R<\/code><\/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>&quot;PAHNAPLSIIGYIR&quot;<\/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><code class=\"language-java\">string convert(string s, int numRows);<\/code><\/pre>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">\u8f93\u5165\uff1as = &quot;PAYPALISHIRING&quot;, numRows = 3\n\u8f93\u51fa\uff1a&quot;PAHNAPLSIIGYIR&quot;<\/code><\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">\u8f93\u5165\uff1as = &quot;PAYPALISHIRING&quot;, numRows = 4\n\u8f93\u51fa\uff1a&quot;PINALSIGYAHRPI&quot;\n\u89e3\u91ca\uff1a\nP     I    N\nA   L S  I G\nY A   H R\nP     I<\/code><\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><code class=\"language-shell\">\u8f93\u5165\uff1as = &quot;A&quot;, numRows = 1\n\u8f93\u51fa\uff1a&quot;A&quot;<\/code><\/pre>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= s.length &lt;= 1000<\/code><\/li>\n<li><code>s<\/code> \u7531\u82f1\u6587\u5b57\u6bcd(\u5c0f\u5199\u548c\u5927\u5199)\u3001<code>&#039;,&#039;<\/code> \u548c <code>&#039;.&#039;<\/code> \u7ec4\u6210<\/li>\n<li><code>1 &lt;= numRows &lt;= 1000<\/code><\/li>\n<\/ul>\n<p>\u7b97\u6cd5\u5b9e\u73b0\u601d\u8def\uff1a<\/p>\n<p>\u6211\u4eec\u628a\u5b57\u7b26\u4e32<code>s<\/code>\u5f53\u505a\u4e00\u4e2a\u6570\u7ec4\uff0c\u8fd9\u4e2a\u6570\u7ec4\u4e0b\u6807\u4ece\u96f6\u5f00\u59cb\u8ba1\u6570\uff0c\u4e00\u76f4\u5230<code>s<\/code>\u7684\u957f\u5ea6\u3002\u6309\u7167Z\u8f6c\u6362\u7684\u601d\u60f3\uff0c\u628a<code>s<\/code>\u7684\u4e0b\u6807\u586b\u5230\u8868\u683c\u4e2d\u3002<\/p>\n<p>\u5f53<code>numRows=3<\/code>\u7684\u65f6\u5019\uff0c\u6392\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/07\/07\/image-20220707150657666.png\" alt=\"image-20220707150657666\" \/><\/p>\n<p>\u5f53<code>numRows=6<\/code>\u7684\u65f6\u5019\uff0c\u6392\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/07\/07\/image-20220707150741906.png\" alt=\"image-20220707150741906\" \/><\/p>\n<p>\u5f53<code>numRows=n+1<\/code>\u7684\u65f6\u5019\uff0c\u6392\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2022\/07\/07\/image-20220707150813523.png\" alt=\"image-20220707150813523\" \/><\/p>\n<p>\u6211\u4eec\u4ece\u7b2c\u96f6\u884c\u5f00\u59cb\u53d6\u5b57\u7b26\uff0c\u5148\u53d6\u7b2c<code>0<\/code>\u4e2a\uff0c\u7136\u540e\u53d6\u7b2c<code>2n<\/code>\u4e2a\uff0c\u7136\u540e\u53d6<code>4n<\/code>\u4e2a\uff0c\u4e00\u76f4\u53d6\u5230\u5b57\u7b26\u4e32<code>s<\/code>\u53ef\u4ee5\u53d6\u5230\u7684\u6700\u5927\u957f\u5ea6\u3002\u63a5\u7740\u53bb\u7b2c\u4e00\u884c\uff0c\u5148\u53d6\u7b2c<code>1<\/code>\u4e2a\uff0c\u7136\u540e\u53d6\u7b2c<code>2n-1<\/code>\u4e2a\uff0c\u7136\u540e\u53d6\u7b2c<code>2n+1<\/code>\u4e2a\uff0c\u4e00\u76f4\u53d6\u5230\u5b57\u7b26\u4e32<code>s<\/code>\u53ef\u4ee5\u53d6\u5230\u7684\u6700\u5927\u957f\u5ea6\u3002\u6309\u7167\u8fd9\u4e2a\u601d\u8def\u4e00\u76f4\u53d6\u5230\u7b2c<code>n<\/code>\u884c\u3002\u56e0\u4e3a\u6211\u4eec\u89c4\u5b9a<code>numRows=n+1<\/code>\uff0c\u6700\u540e\u4e00\u884c\u5c31\u662f\u7b2c<code>n<\/code>\u884c\u3002<\/p>\n<p>\u6211\u4eec\u8bbe\u7f6e\u53d8\u91cf<code>i<\/code>\u4ece<code>0<\/code>\u5230<code>n<\/code>\u8fdb\u884c\u5faa\u73af\uff0c\u6807\u8bb0\u4e3a\u54ea\u4e00\u884c\u3002\u8bbe\u7f6e\u53d8\u91cf<code>j<\/code>\uff0c<code>j=0,2,4,6,8, ... <\/code>\u4e4b\u6240\u4ee5\u8fd9\u6837\u8bbe\u7f6e\uff0c\u662f\u56e0\u4e3a\u65b9\u4fbf\u7b97\u67d0\u4e00\u884c\u7684\u4e0b\u6807\u4f4d\u7f6e\u3002<code>j * n + i<\/code>\u4e3a\u5f53\u524d\u5143\u7d20\uff0c\u90a3\u4e48<code>(j + 2) * n - i<\/code>\u5c31\u662f\u7d27\u63a5\u7740\u5b83\u7684\u540e\u9762\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u5047\u5982\u6709\u7684\u8bdd\u3002\u9650\u5236<code>j * n + i<\/code>\u548c<code>(j + 2) * n - i<\/code>\u90fd\u5c0f\u4e8e<code>s<\/code>\u7684\u957f\u5ea6\u5373\u53ef\u3002\u65f6\u95f4\u590d\u6742\u5ea6O(n<sup>2<\/sup>)\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6O(1)\u3002<\/p>\n<pre><code class=\"language-java\">class Solution {\n    public  String convert(String s, int numRows) {\n\n        int n = numRows - 1;\n        String res = &quot;&quot;;\n\n        if(numRows == 1){\n            return s;\n        }\n\n        for(int i = 0; i &lt; numRows; i++){\n\n            for(int j = 0; j * n + i &lt; s.length(); j = j + 2){\n                \/\/j * n + i\u4e3a\u5f53\u524d\u5143\u7d20\n                if(j * n + i &lt; s.length()){\n                    res = res + String.valueOf(s.charAt(j * n + i));\n                }\n                \/\/(j + 2) * n - i\u662fj * n + i\u540e\u9762\u7d27\u6328\u7740\u7684\u4e00\u4e2a\u5143\u7d20\n                \/\/\u5254\u9664\u7b2c\u96f6\u884c\u548c\u7b2cn\u884c\u91cd\u590d\u7684\u5b57\u7b26\u5143\u7d20 i != 0 &amp;&amp; i != n\n                if((j + 2) * n - i &lt; s.length() &amp;&amp; i != 0 &amp;&amp; i != n){\n                    res = res + String.valueOf(s.charAt((j + 2) * n - i));\n                }\n\n            }\n\n        }\n\n        return res;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u666f\u54e5\u54e5\u535a\u5ba2 \u4ee3\u7801\u4ed3\u5e93 6. Z \u5b57\u5f62\u53d8\u6362 \u5c06\u4e00\u4e2a\u7ed9\u5b9a\u5b57\u7b26\u4e32 s \u6839\u636e\u7ed9\u5b9a\u7684\u884c\u6570 numRows \uff0c\u4ee5\u4ece\u4e0a\u5f80\u4e0b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-345","post","type-post","status-publish","format-standard","hentry","category-leecode"],"_links":{"self":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345"}],"version-history":[{"count":1,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":467,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/345\/revisions\/467"}],"wp:attachment":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}