{"id":862,"date":"2025-08-08T01:01:41","date_gmt":"2025-08-07T17:01:41","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=862"},"modified":"2025-08-08T01:01:41","modified_gmt":"2025-08-07T17:01:41","slug":"leetcode-283-%e7%a7%bb%e5%8a%a8%e9%9b%b6","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=862","title":{"rendered":"LeetCode 283 &#8211; \u79fb\u52a8\u96f6"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 nums\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u5c06\u6240\u6709 0 \u79fb\u52a8\u5230\u6570\u7ec4\u7684\u672b\u5c3e\uff0c\u540c\u65f6\u4fdd\u6301\u975e\u96f6\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f \uff0c\u5fc5\u987b\u5728\u4e0d\u590d\u5236\u6570\u7ec4\u7684\u60c5\u51b5\u4e0b\u539f\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"line-numbers\">\u793a\u4f8b 1:\n\u8f93\u5165: nums = [0,1,0,3,12]\n\u8f93\u51fa: [1,3,12,0,0]\n\n\u793a\u4f8b 2:\n\u8f93\u5165: nums = [0]\n\u8f93\u51fa: [0]\n<\/code><\/pre>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<h2>\u53cc\u6307\u9488\u5192\u6ce1<\/h2>\n<p>\u5c06\u6240\u6709 0 \u79fb\u52a8\u5230\u672b\u5c3e\uff0c\u5f88\u5bb9\u6613\u8054\u60f3\u5230\u5192\u6ce1\u6392\u5e8f\uff0c\u4f46\u5982\u679c\u5148\u628a\u7b2c\u4e00\u4e2a 0 \u79fb\u52a8\u5230\u6700\u540e\uff0c\u518d\u6765\u5904\u7406\u4e0b\u4e00\u4e2a 0 \uff0c\u5c31\u9700\u8981 2 \u5c42\u5faa\u73af\uff0c\u65f6\u95f4\u590d\u6742\u5ea6 <code>O(n^2)<\/code><br \/>\n\u66f4\u597d\u7684\u601d\u8def\u662f\uff0c\u4f7f\u7528\u53cc\u6307\u9488 left \u548c right\uff0cleft \u6307\u5411\u5f53\u524d\u7b2c\u4e00\u4e2a0\uff0cright \u6307\u5411\u5f53\u524d\u7b2c\u4e00\u4e2a\u5728 0 \u540e\u7684\u975e 0 \u6570\u5b57\uff0c\u8ba9\u8fd9\u4e24\u4e2a\u6570\u5b57\u4e92\u6362\u4f4d\u7f6e\u3002<br \/>\nleft \u548c right \u521d\u59cb\u503c\u90fd\u662f 0\uff0c\u5982\u679c\u5f53\u524d\u6570\u5b57\u662f 0 \u5219 right \u524d\u8fdb\u4e00\u4f4d\uff0c\u5982\u679c\u662f\u975e0\u5219 left \u548c right \u90fd\u524d\u8fdb\u4e00\u4f4d\u3002\u6bcf\u6b21\u53d1\u751f\u6570\u5b57\u4e92\u6362\u540e\uff0cleft \u548c right \u90fd\u524d\u8fdb\u4e00\u4f4d\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6  <code>O(n)<\/code><\/p>\n<h2>\u539f\u5730\u91cd\u5199\uff08\u63a8\u8350\uff09<\/h2>\n<p>\u904d\u5386\u6570\u7ec4\uff0c\u8bb0\u5f55\u975e0\u6570\u5b57\u7684\u4e2a\u6570 p\uff0c\u5982\u679c\u5f53\u524d\u662f\u975e0\u6570\u5b57\uff0c\u76f4\u63a5\u628a\u5b83\u653e\u5728\u9884\u671f\u7684\u4f4d\u7f6e p-1\u3002\u5168\u90e8\u904d\u5386\u5b8c\u4e4b\u540e\uff0c\u518d\u628a >=p \u7684\u4f4d\u7f6e\u90fd\u4fee\u6539\u6210 0\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6  <code>O(n)<\/code><\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public void moveZeroes1(int[] nums) {\n    int left = 0;\n    int right = 0;\n    while (right &lt; nums.length) {\n        if (nums[right] != 0) {\n            if (left != right) {\n                int t = nums[left];\n                nums[left] = nums[right];\n                nums[right] = t;\n            }\n            left++;\n        }\n        right++;\n    }\n}\n\npublic void moveZeroes2(int[] nums) {\n    int p = 0; \/\/ \u975e0\u6570\u5b57\u603b\u6570\n    for (int i = 0; i &lt; nums.length; i++) {\n        if (nums[i] != 0) {\n            p++;\n            nums[p - 1] = nums[i];\n        }\n    }\n    for (int i = p; i &lt; nums.length; i++) {\n        nums[i] = 0;\n    }\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun moveZeroes1(nums: IntArray) {\n    var left = 0\n    var right = 0\n    while (right &lt; nums.size) {\n        if (nums[right] != 0) {\n            if (left != right) {\n                val t = nums[left]\n                nums[left] = nums[right]\n                nums[right] = t\n            }\n            left++\n        }\n        right++\n    }\n}\n\nfun moveZeroes2(nums: IntArray) {\n    var p = 0 \/\/ \u975e0\u6570\u5b57\u603b\u6570\n    for (i in nums.indices) {\n        if (nums[i] != 0) {\n            p++\n            nums[p - 1] = nums[i]\n        }\n    }\n    for (i in p until nums.size) {\n        nums[i] = 0\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4 nums\uff0c\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u5c06\u6240\u6709 0 \u79fb\u52a8\u5230\u6570\u7ec4\u7684\u672b\u5c3e\uff0c\u540c\u65f6\u4fdd\u6301\u975e\u96f6\u5143\u7d20\u7684\u76f8\u5bf9\u987a\u5e8f\u3002 \u8bf7\u6ce8 [&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,75,73],"class_list":["post-862","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-75","tag-73"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/862","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=862"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions\/863"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}