{"id":906,"date":"2025-10-25T16:55:34","date_gmt":"2025-10-25T08:55:34","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=906"},"modified":"2025-10-25T16:55:34","modified_gmt":"2025-10-25T08:55:34","slug":"leetcode-48-%e6%97%8b%e8%bd%ac%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=906","title":{"rendered":"LeetCode 48 \u2013 \u65cb\u8f6c\u56fe\u50cf"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a n \u00d7 n \u7684\u4e8c\u7ef4\u77e9\u9635 matrix \u8868\u793a\u4e00\u4e2a\u56fe\u50cf\u3002\u8bf7\u4f60\u5c06\u56fe\u50cf\u987a\u65f6\u9488\u65cb\u8f6c 90 \u5ea6\u3002<br \/>\n\u4f60\u5fc5\u987b\u5728 \u539f\u5730 \u65cb\u8f6c\u56fe\u50cf\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u76f4\u63a5\u4fee\u6539\u8f93\u5165\u7684\u4e8c\u7ef4\u77e9\u9635\u3002\u8bf7\u4e0d\u8981 \u4f7f\u7528\u53e6\u4e00\u4e2a\u77e9\u9635\u6765\u65cb\u8f6c\u56fe\u50cf\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_c7b21e149e7ab8ac5248fa41751fe60f.jpg\"><img decoding=\"async\" src=\"https:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2025\/10\/wp_editor_md_c7b21e149e7ab8ac5248fa41751fe60f.jpg\" alt=\"\" \/><\/a><br \/>\n\u8f93\u5165\uff1amatrix = [[1,2,3],[4,5,6],[7,8,9]]<br \/>\n\u8f93\u51fa\uff1a[[7,4,1],[8,5,2],[9,6,3]]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u5faa\u73af\u66ff\u6362<\/h2>\n<p>\u56f4\u7ed5\u4e2d\u5fc3\u70b9\uff0c\u628a\u77e9\u9635\u5206\u62104\u4e2a\u5c0f\u77e9\u9635\uff1a\u5de6\u4e0a\u3001\u53f3\u4e0a\u3001\u53f3\u4e0b\u3001\u5de6\u4e0b<br \/>\n\u679a\u4e3e\u5de6\u4e0a\u89d2\u7684\u5c0f\u77e9\u9635\u5185\u7684\u5143\u7d20\uff0c\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5faa\u73af\u65cb\u8f6c\uff0c\u5bf9\u5e944\u4e2a\u6570\u5b57<\/p>\n<h2>2\u6b65\u4ea4\u6362<\/h2>\n<p>\u628a\u987a\u65f6\u9488\u65cb\u8f6c\u62c6\u5206\u6210 2 \u6b65\u4ea4\u6362\uff1a<br \/>\n&#8211; \u5de6\u4e0a\u548c\u53f3\u4e0b\u4ea4\u6362\u4e00\u6b21\uff08\u5ef6\u5bf9\u89d2\u7ebf\u5bf9\u6298\uff09<br \/>\n&#8211; \u6574\u4f53\u4e0a\u4e0b\u4ea4\u6362\u4e00\u6b21\uff08\u5ef6\u6c34\u5e73\u7ebf\u5bf9\u6298\uff09<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public void rotate1(int[][] matrix) {\n    int n = matrix.length;\n    for(int i = 0; i &lt; n \/ 2; i++) {\n        for(int j = 0; j &lt; (n + 1) \/ 2; j++){\n            int temp = matrix[i][j];\n            matrix[i][j] = matrix[n - j - 1][i];\n            matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];\n            matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];\n            matrix[j][n - i - 1] = temp;\n        }\n    }\n}\n\npublic void rotate2(int[][] matrix) {\n    int n = matrix.length;\n    for (int i = 0; i &lt; n; i++) {\n        for (int j = 0; i + j &lt; n; j++) {\n            swap(matrix, i, j, n - j - 1, n - i - 1);\n        }\n    }\n\n    for (int i = 0; i &lt; n \/ 2; i++) {\n        for (int j = 0; j &lt; n; j++) {\n            swap(matrix, i, j, n - i - 1, j);\n        }\n    }\n}\n\nprivate void swap(int[][] matrix, int x1, int y1, int x2, int y2) {\n    int temp = matrix[x1][y1];\n    matrix[x1][y1] = matrix[x2][y2];\n    matrix[x2][y2] = temp;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun rotate1(matrix: Array&lt;IntArray&gt;): Unit {\n    val n = matrix.size\n    for (i in 0 until n \/ 2) {\n        for (j in 0 until (n + 1) \/ 2) {\n            val temp = matrix[i][j]\n            matrix[i][j] = matrix[n - j - 1][i]\n            matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]\n            matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]\n            matrix[j][n - i - 1] = temp\n        }\n    }\n}\n\nfun rotate2(matrix: Array&lt;IntArray&gt;): Unit {\n    val n = matrix.size\n    for (i in 0 until n) {\n        for (j in 0 until n - i) {\n            swap1(matrix, i, j, n - 1 - j, n - 1 - i)\n        }\n    }\n    for (i in 0 until n \/ 2) {\n        for (j in 0 until n) {\n            swap1(matrix, i, j, n - i - 1, j)\n        }\n    }\n}\n\nprivate fun swap1(matrix: Array&lt;IntArray&gt;, x1: Int, y1: Int, x2: Int, y2: Int) {\n    val temp = matrix[x1][y1]\n    matrix[x1][y1] = matrix[x2][y2]\n    matrix[x2][y2] = temp\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a n \u00d7 n \u7684\u4e8c\u7ef4\u77e9\u9635 matrix \u8868\u793a\u4e00\u4e2a\u56fe\u50cf\u3002\u8bf7\u4f60\u5c06\u56fe\u50cf\u987a\u65f6\u9488\u65cb\u8f6c 90 \u5ea6\u3002 \u4f60\u5fc5 [&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,82,73,83],"class_list":["post-906","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-82","tag-73","tag-83"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/906","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=906"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/906\/revisions"}],"predecessor-version":[{"id":907,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/906\/revisions\/907"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}