{"id":302,"date":"2025-06-27T14:50:00","date_gmt":"2025-06-27T06:50:00","guid":{"rendered":"http:\/\/getzs.online\/?p=302"},"modified":"2025-06-30T22:35:26","modified_gmt":"2025-06-30T14:35:26","slug":"80-%e5%88%a0%e9%99%a4%e6%9c%89%e5%ba%8f%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e9%87%8d%e5%a4%8d%e9%a1%b9-ii","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/27\/80-%e5%88%a0%e9%99%a4%e6%9c%89%e5%ba%8f%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e9%87%8d%e5%a4%8d%e9%a1%b9-ii\/","title":{"rendered":"80. \u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879 II"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/remove-duplicates-from-sorted-array-ii\/\">80. \u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879 II<\/a><\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u8bf7\u4f60<strong><a href=\"http:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\" rel=\"noreferrer noopener\">&nbsp;\u539f\u5730<\/a><\/strong>&nbsp;\u5220\u9664\u91cd\u590d\u51fa\u73b0\u7684\u5143\u7d20\uff0c\u4f7f\u5f97\u51fa\u73b0\u6b21\u6570\u8d85\u8fc7\u4e24\u6b21\u7684\u5143\u7d20<strong>\u53ea\u51fa\u73b0\u4e24\u6b21<\/strong>&nbsp;\uff0c\u8fd4\u56de\u5220\u9664\u540e\u6570\u7ec4\u7684\u65b0\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u4e0d\u8981\u4f7f\u7528\u989d\u5916\u7684\u6570\u7ec4\u7a7a\u95f4\uff0c\u4f60\u5fc5\u987b\u5728&nbsp;<strong><a href=\"https:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\" rel=\"noreferrer noopener\">\u539f\u5730&nbsp;<\/a>\u4fee\u6539\u8f93\u5165\u6570\u7ec4&nbsp;<\/strong>\u5e76\u5728\u4f7f\u7528 O(1) \u989d\u5916\u7a7a\u95f4\u7684\u6761\u4ef6\u4e0b\u5b8c\u6210\u3002<\/p>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8fd4\u56de\u6570\u503c\u662f\u6574\u6570\uff0c\u4f46\u8f93\u51fa\u7684\u7b54\u6848\u662f\u6570\u7ec4\u5462\uff1f<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u8f93\u5165\u6570\u7ec4\u662f\u4ee5<strong>\u300c\u5f15\u7528\u300d<\/strong>\u65b9\u5f0f\u4f20\u9012\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5728\u51fd\u6570\u91cc\u4fee\u6539\u8f93\u5165\u6570\u7ec4\u5bf9\u4e8e\u8c03\u7528\u8005\u662f\u53ef\u89c1\u7684\u3002<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u60f3\u8c61\u5185\u90e8\u64cd\u4f5c\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ <strong>nums<\/strong> \u662f\u4ee5\u201c\u5f15\u7528\u201d\u65b9\u5f0f\u4f20\u9012\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e0d\u5bf9\u5b9e\u53c2\u505a\u4efb\u4f55\u62f7\u8d1d\nint len = removeDuplicates(nums);\n\n\/\/ \u5728\u51fd\u6570\u91cc\u4fee\u6539\u8f93\u5165\u6570\u7ec4\u5bf9\u4e8e\u8c03\u7528\u8005\u662f\u53ef\u89c1\u7684\u3002\n\/\/ \u6839\u636e\u4f60\u7684\u51fd\u6570\u8fd4\u56de\u7684\u957f\u5ea6, \u5b83\u4f1a\u6253\u5370\u51fa\u6570\u7ec4\u4e2d<strong> \u8be5\u957f\u5ea6\u8303\u56f4\u5185<\/strong> \u7684\u6240\u6709\u5143\u7d20\u3002\nfor (int i = 0; i &lt; len; i++) {\n&nbsp; &nbsp; print(nums[i]);\n}\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,1,1,2,2,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>5, nums = [1,1,2,2,3]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u51fd\u6570\u5e94\u8fd4\u56de\u65b0\u957f\u5ea6 length = <strong><code>5<\/code><\/strong>, \u5e76\u4e14\u539f\u6570\u7ec4\u7684\u524d\u4e94\u4e2a\u5143\u7d20\u88ab\u4fee\u6539\u4e3a <strong><code>1, 1, 2, 2, 3<\/code><\/strong>\u3002 \u4e0d\u9700\u8981\u8003\u8651\u6570\u7ec4\u4e2d\u8d85\u51fa\u65b0\u957f\u5ea6\u540e\u9762\u7684\u5143\u7d20\u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>nums = [0,0,1,1,1,1,2,3,3]\n<strong>\u8f93\u51fa\uff1a<\/strong>7, nums = [0,0,1,1,2,3,3]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u51fd\u6570\u5e94\u8fd4\u56de\u65b0\u957f\u5ea6 length = <strong><code>7<\/code><\/strong>, \u5e76\u4e14\u539f\u6570\u7ec4\u7684\u524d\u4e03\u4e2a\u5143\u7d20\u88ab\u4fee\u6539\u4e3a&nbsp;<strong><code>0, 0, 1, 1, 2, 3, 3<\/code><\/strong>\u3002\u4e0d\u9700\u8981\u8003\u8651\u6570\u7ec4\u4e2d\u8d85\u51fa\u65b0\u957f\u5ea6\u540e\u9762\u7684\u5143\u7d20\u3002\n<\/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;= 3 * 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>-10<sup>4<\/sup> &lt;= nums[i] &lt;= 10<sup>4<\/sup><\/code><\/li>\n\n\n\n<li><code>nums<\/code>\u00a0\u5df2\u6309\u5347\u5e8f\u6392\u5217<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">public int removeDuplicates(int[] nums) {<br>    return method1(nums, nums.length, 2);<br>}<br><br><em>\/**<br><\/em><em> * <\/em><em>\u53cc\u6307\u9488 <\/em><em>-&gt; <\/em><em>\u975e\u4e25\u683c\u9012\u589e<br><\/em><em> <\/em><em>* 0ms<br><\/em><em> * <\/em><em>\u590d\u6742\u5ea6<\/em><em>O(n)<br><\/em><em> *\/<br><\/em>public int method1(int[] nums, int n, int mx) {<br>    int i, j;<br>    i = j = 0;<br>    while (j &lt; n) {<br>        int k = j;<br>        \/\/ \u5224\u65ad\u91cd\u590d\u5143\u7d20<br>        while (j &lt; n &amp;&amp; nums[j] == nums[k]) {<br>            \/\/ \u5224\u65ad\u6b65\u6570\u662f\u5426\u8d85\u8fc7\u9608\u503c\uff0c\u6ca1\u64cd\u8fc7\u5219\u8d4b\u503c<br>            if (j - k &lt; mx) {<br>                nums[i++] = nums[k];<br>            }<br>            j++;<br>        }<br>    }<br>    return i;<br>}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>80. \u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879 II \u7ed9\u4f60\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4&nbsp;nums&nbsp;\uff0c\u8bf7\u4f60&nbsp;\u539f\u5730&#038; [&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-302","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/302","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=302"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":303,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/302\/revisions\/303"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}