{"id":904,"date":"2025-10-25T15:51:08","date_gmt":"2025-10-25T07:51:08","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=904"},"modified":"2025-10-25T15:51:08","modified_gmt":"2025-10-25T07:51:08","slug":"leetcode-54-%e8%9e%ba%e6%97%8b%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=904","title":{"rendered":"LeetCode 54 \u2013 \u87ba\u65cb\u77e9\u9635"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u4f60\u4e00\u4e2a m \u884c n \u5217\u7684\u77e9\u9635 matrix \uff0c\u8bf7\u6309\u7167 \u987a\u65f6\u9488\u87ba\u65cb\u987a\u5e8f \uff0c\u8fd4\u56de\u77e9\u9635\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p>\u793a\u4f8b1<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_5536d3885212a4edb4919372bdcd04de.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_5536d3885212a4edb4919372bdcd04de.jpg\" alt=\"\" \/><\/a><br \/>\n\u8f93\u5165\uff1amatrix = [[1,2,3],[4,5,6],[7,8,9]]<br \/>\n\u8f93\u51fa\uff1a[1,2,3,6,9,8,7,4,5]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u6a21\u62df.<br \/>\n\u4f7f\u7528 directions \u8868\u793a\u4e0d\u540c\u65b9\u5411\u5bf9\u5e94\u7684\u4f4d\u79fb\uff0c\u6309 \u53f3\u3001\u4e0b\u3001\u5de6\u3001\u4e0a \u7684\u987a\u5e8f\u6392\u5217\uff0c\u4f7f\u7528 index \u8868\u793a\u5f53\u524d\u65b9\u5411\uff0c\u5219 (index + 1) % 4 \u4e3a\u4e0b\u4e00\u4e2a\u65b9\u5411\u3002<br \/>\n\u4f7f\u7528 visit \u6570\u7ec4\u5bf9\u8bbf\u95ee\u8fc7\u7684\u4f4d\u7f6e\u505a\u6807\u8bb0\uff0c\u6309\u7167\u5f53\u524d\u65b9\u5411\u4e0d\u65ad\u79fb\u52a8\uff0c\u5982\u679c\u5931\u8d25\u5219\u6362\u5230\u4e0b\u4e00\u4e2a\u65b9\u5411\u3002<br \/>\n\u5f53 ans \u91cc\u7684\u6570\u91cf\u548c\u6570\u5b57\u603b\u6570\u76f8\u540c\u65f6\uff0c\u904d\u5386\u7ed3\u675f\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public List&lt;Integer&gt; spiralOrder(int[][] matrix) {\n    int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n    int index = 0;\n    int m = matrix.length;\n    int n = matrix[0].length;\n    boolean[][] visit = new boolean[m][n];\n    List&lt;Integer&gt; ans = new ArrayList&lt;&gt;();\n    int i = 0, j = -1;\n    while (ans.size() &lt; m * n) {\n        int nextI = i + directions[index][0];\n        int nextJ = j + directions[index][1];\n        if (nextI &gt;= 0 &amp;&amp; nextI &lt; m &amp;&amp; nextJ &gt;= 0 &amp;&amp; nextJ &lt; n &amp;&amp; !visit[nextI][nextJ]) {\n            i = nextI;\n            j = nextJ;\n            ans.add(matrix[i][j]);\n            visit[i][j] = true;\n        } else {\n            index = (index + 1) % 4;\n        }\n    }\n    return ans;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun spiralOrder(matrix: Array&lt;IntArray&gt;): List&lt;Int&gt; {\n    val ans = mutableListOf&lt;Int&gt;()\n    val m = matrix.size\n    val n = matrix[0].size\n    val directions = listOf(intArrayOf(0, 1), intArrayOf(1, 0), intArrayOf(0, -1), intArrayOf(-1, 0))\n    val visit = Array(m) { BooleanArray(n) }\n    var index = 0\n    var i = 0\n    var j = -1\n    while (ans.size &lt; m * n) {\n        val nextI = i + directions[index][0]\n        val nextJ = j + directions[index][1]\n        if (nextI in 0 until m &amp;&amp; nextJ in 0 until n &amp;&amp; !visit[nextI][nextJ]) {\n            i = nextI\n            j = nextJ\n            visit[i][j] = true\n            ans.add(matrix[i][j])\n        } else {\n            index = (index + 1) % 4\n        }\n    }\n\n    return ans\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u4f60\u4e00\u4e2a m \u884c n \u5217\u7684\u77e9\u9635 matrix \uff0c\u8bf7\u6309\u7167 \u987a\u65f6\u9488\u87ba\u65cb\u987a\u5e8f \uff0c\u8fd4\u56de\u77e9\u9635\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002 \u793a [&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":[20,73,54,83],"class_list":["post-904","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-73","tag-54","tag-83"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/904","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=904"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/904\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}