{"id":727,"date":"2020-06-28T21:48:42","date_gmt":"2020-06-28T13:48:42","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=727"},"modified":"2020-06-28T21:48:42","modified_gmt":"2020-06-28T13:48:42","slug":"jz19-%e9%a1%ba%e6%97%b6%e9%92%88%e6%89%93%e5%8d%b0%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=727","title":{"rendered":"JZ19 \u2014 \u987a\u65f6\u9488\u6253\u5370\u77e9\u9635"},"content":{"rendered":"<h3>\u9898\u76ee\u63cf\u8ff0<\/h3>\n<p>\u8f93\u5165\u4e00\u4e2a\u77e9\u9635\uff0c\u6309\u7167\u4ece\u5916\u5411\u91cc\u4ee5\u987a\u65f6\u9488\u7684\u987a\u5e8f\u4f9d\u6b21\u6253\u5370\u51fa\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u8f93\u5165\u5982\u4e0b4 X 4\u77e9\u9635\uff1a<\/p>\n<pre><code class=\"language-cpp line-numbers\">1  2  3  4\n5  6  7  8\n9  10 11 12\n13 14 15 16\n<\/code><\/pre>\n<p>\u5219\u4f9d\u6b21\u6253\u5370\u51fa\u6570\u5b571,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.<\/p>\n<h3>\u9898\u76ee\u5206\u6790<\/h3>\n<h3>\u89e3\u6cd5\u4e00<\/h3>\n<p>\u5199\u4e00\u4e2a\u8ba9\u77e9\u9635\u65cb\u8f6c\u7684\u51fd\u6570\uff0c\u6bcf\u6b21\u9006\u65f6\u9488\u65cb\u8f6c90\u5ea6\u3002\u7136\u540e\u6bcf\u6b21\u8f93\u51fa\u7b2c\u4e00\u884c\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-cpp line-numbers\">\/\/ \u539f\u77e9\u9635\n1  2  3  4\n5  6  7  8\n9  10 11 12\n13 14 15 16\n\n\/\/ \u8f93\u51fa\u7b2c\u4e00\u884c\u540e\n5  6  7  8\n9  10 11 12\n13 14 15 16\n\n\/\/ \u9006\u65f6\u9488\u65cb\u8f6c90\u5ea6\n8  12 12\n7  11 15\n6  10 14\n5  9  13\n\n\/\/ \u8f93\u51fa\u7b2c\u4e00\u884c\u540e\n7  11 15\n6  10 14\n5  9  13\n<\/code><\/pre>\n<p>\u4f46\u8fd9\u79cd\u64cd\u4f5c\u6548\u7387\u592a\u4f4e\uff0c\u65cb\u8f6c\u4e00\u6b21\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a<code>O(m*n)<\/code>\u3002<\/p>\n<h3>\u89e3\u6cd5\u4e8c<\/h3>\n<p>\u4ece\u5916\u5411\u5185\u4ee5\u987a\u65f6\u9488\u987a\u5e8f\u8f93\u51fa\uff0c\u53ef\u4ee5\u5206\u89e3\u4e3a\u4e00\u5c42\u4e00\u5c42\u7684\u8f93\u51fa\u3002<br \/>\n\u5177\u4f53\u7b56\u7565\u662f\uff1a\u4ece\u5916\u5411\u5185\u6bcf\u6b21\u8f93\u51fa\u4e00\u5c42\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u5c42\uff0c\u91c7\u7528<code>\u4e0a\u3001\u53f3\u3001\u4e0b\u3001\u5de6<\/code>\u8fd9\u6837\u7684\u987a\u5e8f\u8fdb\u884c\u987a\u65f6\u9488\u6253\u5370\u3002\u5982\u679c\u53ea\u6709\u4e00\u884c\uff0c\u5219\u8f93\u51fa<code>\u4e0a<\/code>\uff1b\u5982\u679c\u53ea\u6709\u4e00\u5217\uff0c\u5219\u8f93\u51fa<code>\u4e0a\u3001\u53f3<\/code>\uff1b<br \/>\n<img decoding=\"async\" src=\"https:\/\/uploadfiles.nowcoder.com\/images\/20200418\/284295_1587193215624_F2282F9F07FF5D0E05D17985D7C0140C\" alt=\"\" \/><\/p>\n<pre><code class=\"language-cpp line-numbers\">\/\/ \u539f\u77e9\u9635\n1  2  3  4\n5  6  7  8\n9  10 11 12\n13 14 15 16\n\n\/\/ \u8f93\u51fa\u6700\u5916\u4e00\u5c42\u540e\n6  7\n10 11\n<\/code><\/pre>\n<h3>C++<\/h3>\n<pre><code class=\"language-cpp line-numbers\">class Solution\n{\npublic:\n    vector&lt;int&gt; printMatrix(vector&lt;vector&lt;int&gt; &gt; matrix)\n    {\n        vector&lt;int&gt; ans;\n        int x1, y1, x2, y2;\n        for (x1 = y1 = 0, x2 = matrix.size() - 1, y2 = matrix[0].size() - 1; x1 &lt;= x2 &amp;&amp; y1 &lt;= y2; x1++, y1++, x2--, y2--)\n        {\n            \/\/\u4e0a\n            for (int y = y1; y &lt;= y2; y++)\n                ans.push_back(matrix[x1][y]);\n            if (x1 == x2) \/\/\u53ea\u6709\u4e00\u884c\n                break;\n            \/\/\u53f3\n            for (int x = x1 + 1; x &lt;= x2; x++)\n                ans.push_back(matrix[x][y2]);\n            if (y1 == y2) \/\/\u53ea\u6709\u4e00\u5217\n                break;\n            \/\/\u4e0b\n            for (int y = y2 - 1; y &gt;= y1; y--)\n                ans.push_back(matrix[x2][y]);\n            \/\/\u5de6\n            for (int x = x2 - 1; x &gt; x1; x--)\n                ans.push_back(matrix[x][y1]);\n        }\n        return ans;\n    }\n};\n<\/code><\/pre>\n<h3>Java<\/h3>\n<pre><code class=\"language-java line-numbers\">import java.util.ArrayList;\npublic class Solution {\n    public ArrayList&lt;Integer&gt; printMatrix(int[][] matrix) {\n        ArrayList&lt;Integer&gt; ans = new ArrayList&lt;&gt;();\n        int x1, x2, y1, y2;\n        for (x1 = y1 = 0, x2 = matrix.length - 1, y2 = matrix[0].length - 1; x1 &lt;= x2\n                &amp;&amp; y1 &lt;= y2; x1++, y1++, x2--, y2--) {\n            \/\/ \u4e0a\n            for (int y = y1; y &lt;= y2; y++)\n                ans.add(matrix[x1][y]);\n            if (x1 == x2)\/\/ \u53ea\u6709\u4e00\u884c\n                break;\n            \/\/ \u53f3\n            for (int x = x1 + 1; x &lt;= x2; x++)\n                ans.add(matrix[x][y2]);\n            if (y1 == y2)\/\/ \u53ea\u6709\u4e00\u5217\n                break;\n            \/\/ \u4e0b\n            for (int y = y2 - 1; y &gt;= y1; y--)\n                ans.add(matrix[x2][y]);\n            \/\/ \u5de6\n            for (int x = x2 - 1; x &gt; x1; x--)\n                ans.add(matrix[x][y1]);\n        }\n        return ans;\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u8f93\u5165\u4e00\u4e2a\u77e9\u9635\uff0c\u6309\u7167\u4ece\u5916\u5411\u91cc\u4ee5\u987a\u65f6\u9488\u7684\u987a\u5e8f\u4f9d\u6b21\u6253\u5370\u51fa\u6bcf\u4e00\u4e2a\u6570\u5b57\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u8f93\u5165\u5982\u4e0b4 X 4\u77e9\u9635\uff1a 1 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[39],"class_list":["post-727","post","type-post","status-publish","format-standard","hentry","category-algo","tag-offer"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/727","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=727"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/727\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=727"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=727"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=727"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}