{"id":306,"date":"2025-06-28T15:37:00","date_gmt":"2025-06-28T07:37:00","guid":{"rendered":"http:\/\/getzs.online\/?p=306"},"modified":"2025-06-30T22:37:47","modified_gmt":"2025-06-30T14:37:47","slug":"189-%e8%bd%ae%e8%bd%ac%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/28\/189-%e8%bd%ae%e8%bd%ac%e6%95%b0%e7%bb%84\/","title":{"rendered":"189. \u8f6e\u8f6c\u6570\u7ec4"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/rotate-array\/\">189. \u8f6e\u8f6c\u6570\u7ec4<\/a><\/p>\n\n\n\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;<code>nums<\/code>\uff0c\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5411\u53f3\u8f6e\u8f6c&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u4e2a\u4f4d\u7f6e\uff0c\u5176\u4e2d&nbsp;<code>k<\/code><em>&nbsp;<\/em>\u662f\u975e\u8d1f\u6570\u3002<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165:<\/strong> nums = [1,2,3,4,5,6,7], k = 3\n<strong>\u8f93\u51fa:<\/strong> <code>[5,6,7,1,2,3,4]<\/code>\n<strong>\u89e3\u91ca:<\/strong>\n\u5411\u53f3\u8f6e\u8f6c 1 \u6b65: <code>[7,1,2,3,4,5,6]<\/code>\n\u5411\u53f3\u8f6e\u8f6c 2 \u6b65: <code>[6,7,1,2,3,4,5]\n<\/code>\u5411\u53f3\u8f6e\u8f6c 3 \u6b65: <code>[5,6,7,1,2,3,4]<\/code>\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [-1,-100,3,99], k = 2\n<strong>\u8f93\u51fa\uff1a<\/strong>[3,99,-1,-100]\n<strong>\u89e3\u91ca:<\/strong> \n\u5411\u53f3\u8f6e\u8f6c 1 \u6b65: [99,-1,-100,3]\n\u5411\u53f3\u8f6e\u8f6c 2 \u6b65: [3,99,-1,-100]<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li>\n\n\n\n<li><code>-2<sup>31<\/sup> &lt;= nums[i] &lt;= 2<sup>31<\/sup> - 1<\/code><\/li>\n\n\n\n<li><code>0 &lt;= k &lt;= 10<sup>5<\/sup><\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>\u8fdb\u9636\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c3d\u53ef\u80fd\u60f3\u51fa\u66f4\u591a\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u81f3\u5c11\u6709\u00a0<strong>\u4e09\u79cd<\/strong>\u00a0\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528\u7a7a\u95f4\u590d\u6742\u5ea6\u4e3a\u00a0<code>O(1)<\/code>\u00a0\u7684\u00a0<strong>\u539f\u5730\u00a0<\/strong>\u7b97\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5417\uff1f<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    public void rotate(int&#91;] nums, int k) {\n        int n = nums.length;\n        k %= n;\n        method1(nums, k, n);\n    }\n\n    \/**\n     * \u7ffb\u8f6c\u6570\u7ec4\n     * 0ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public void method1(int&#91;] nums , int k, int n) {\n        \/\/ \u5168\u90e8\u7ffb\u8f6c\n        reverse(nums, 0, n - 1);\n        \/\/ \u7ffb\u8f6c\u524dk\u4e2a\n        reverse(nums, 0, k - 1);\n        \/\/ \u7ffb\u8f6c\u540en-k\u4e2a\n        reverse(nums, k, n - 1);\n    }\n\n    public void reverse(int&#91;]nums, int start, int end) {\n        while (start &lt; end) {\n            int temp = nums&#91;start];\n            nums&#91;start] = nums&#91;end];\n            nums&#91;end] = temp;\n            start++;\n            end--;\n        }\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>189. \u8f6e\u8f6c\u6570\u7ec4 \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4&nbsp;nums\uff0c\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u5411\u53f3\u8f6e\u8f6c&nbsp;k&nbsp;\u4e2a\u4f4d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,11],"tags":[],"class_list":["post-306","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/306\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}