{"id":1056,"date":"2025-12-25T11:21:07","date_gmt":"2025-12-25T03:21:07","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1056"},"modified":"2025-12-25T11:21:07","modified_gmt":"2025-12-25T03:21:07","slug":"leetcode-75-%e9%a2%9c%e8%89%b2%e5%88%86%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1056","title":{"rendered":"LeetCode 75 \u2013 \u989c\u8272\u5206\u7c7b"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u7ea2\u8272\u3001\u767d\u8272\u548c\u84dd\u8272\u3001\u5171 n \u4e2a\u5143\u7d20\u7684\u6570\u7ec4 nums \uff0c\u539f\u5730 \u5bf9\u5b83\u4eec\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u5f97\u76f8\u540c\u989c\u8272\u7684\u5143\u7d20\u76f8\u90bb\uff0c\u5e76\u6309\u7167\u7ea2\u8272\u3001\u767d\u8272\u3001\u84dd\u8272\u987a\u5e8f\u6392\u5217\u3002<br \/>\n\u6211\u4eec\u4f7f\u7528\u6574\u6570 0\u3001 1 \u548c 2 \u5206\u522b\u8868\u793a\u7ea2\u8272\u3001\u767d\u8272\u548c\u84dd\u8272\u3002<br \/>\n\u5fc5\u987b\u5728\u4e0d\u4f7f\u7528\u5e93\u5185\u7f6e\u7684 sort \u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u8fdb\u9636\uff1a<br \/>\n\u4f60\u80fd\u60f3\u51fa\u4e00\u4e2a\u4ec5\u4f7f\u7528\u5e38\u6570\u7a7a\u95f4\u7684\u4e00\u8d9f\u626b\u63cf\u7b97\u6cd5\u5417\uff1f<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1anums = [2,0,2,1,1,0]<br \/>\n\u8f93\u51fa\uff1a[0,0,1,1,2,2]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u5982\u679c\u5141\u8bb8\u626b\u63cf 2 \u904d\uff0c\u53ef\u4ee5\u8bb0\u5f55\u6bcf\u79cd\u7684\u4e2a\u6570\uff0c\u7136\u540e\u8d4b\u503c\u3002<br \/>\n\u5982\u679c\u53ea\u5141\u8bb8\u626b\u63cf 1 \u904d\uff0c\u53ef\u4ee5\u5148\u601d\u8003\u5bf9\u4e8e\u4ec5 2 \u79cd\u5143\u7d20\u7684\u95ee\u9898\u5982\u4f55\u89e3\u51b3\uff1f\u75281\u4e2a\u6307\u9488\u8868\u793a\u7b2c\u4e00\u79cd\u5143\u7d20\u7684\u9884\u671f\u4f4d\u7f6e\uff0c\u904d\u5386\u65f6\u9047\u5230\u7b2c\u4e00\u79cd\u5143\u7d20\u5c31\u548c\u6307\u9488\u8fdb\u884c\u4ea4\u6362\u3002\u7b2c\u4e8c\u79cd\u5143\u7d20\u65e0\u9700\u7279\u6b8a\u5904\u7406\uff0c\u81ea\u7136\u4e5f\u4f1a\u88ab\u79fb\u52a8\u5230\u9884\u671f\u4f4d\u7f6e\u3002<br \/>\n\u5bf9\u4e8e 3 \u79cd\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 2 \u4e2a\u6307\u9488\uff0c\u5206\u522b\u8868\u793a\u7ea2\u8272\u7684\u9884\u671f\u4f4d\u7f6e\uff08\u5de6\u4fa7\uff09\uff0c\u84dd\u8272\u7684\u9884\u671f\u4f4d\u7f6e\uff08\u53f3\u4fa7\uff09\uff0c\u904d\u5386\u6570\u7ec4\u65f6\uff0c\u9047\u5230\u7ea2\u8272\u5c31\u548c\u7ea2\u8272\u6307\u9488\u4ea4\u6362\uff0c\u9047\u5230\u84dd\u8272\u5c31\u548c\u84dd\u8272\u6307\u9488\u4ea4\u6362\u3002\u767d\u8272\u65e0\u9700\u5904\u7406\u3002<br \/>\n\u9700\u8981\u517c\u5bb9\u5355\u4e2a\u4f4d\u7f6e\u4ea4\u6362\u591a\u6b21\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\uff1a\u626b\u63cf\u5230\u4e86\u84dd\u8272\uff0c\u4e0e\u53f3\u4fa7\u6307\u9488\u4ea4\u6362\u540e\u6070\u597d\u662f\u7ea2\u8272\u3002<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public void sortColors(int[] nums) {\n    int red = 0, blue = nums.length - 1;\n    for (int i = 0; i &lt; nums.length; i++) {\n        while (i &gt;= red &amp;&amp; i &lt;= blue) {\n            if (nums[i] == 0) {\n                swap(nums, i, red++);\n            } else if (nums[i] == 2) {\n                swap(nums, i, blue--);\n            } else {\n                break;\n            }\n        }\n    }\n}\n\nprivate void swap(int[] nums, int i, int j) {\n    int temp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = temp;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun sortColors(nums: IntArray): Unit {\n    var red = 0\n    var blue = nums.size - 1\n\n    val swap = { i: Int, j: Int -&gt;\n        val temp = nums[i]\n        nums[i] = nums[j]\n        nums[j] = temp\n    }\n\n    for (i in nums.indices) {\n        while (i in red..blue) {\n            when (nums[i]) {\n                0 -&gt; swap(i, red++)\n                1 -&gt; break\n                2 -&gt; swap(i, blue--)\n            }\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u7ea2\u8272\u3001\u767d\u8272\u548c\u84dd\u8272\u3001\u5171 n \u4e2a\u5143\u7d20\u7684\u6570\u7ec4 nums \uff0c\u539f\u5730 \u5bf9\u5b83\u4eec\u8fdb\u884c\u6392\u5e8f\uff0c\u4f7f\u5f97\u76f8\u540c\u989c\u8272\u7684 [&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,92,51],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-75","tag-92","tag-51"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1056","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=1056"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":1059,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1056\/revisions\/1059"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}