{"id":300,"date":"2025-06-27T14:33:00","date_gmt":"2025-06-27T06:33:00","guid":{"rendered":"http:\/\/getzs.online\/?p=300"},"modified":"2025-06-30T22:34:30","modified_gmt":"2025-06-30T14:34:30","slug":"26-%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","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/27\/26-%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\/","title":{"rendered":"26.\u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/remove-duplicates-from-sorted-array\/\">26. \u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879<\/a><\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a&nbsp;<strong>\u975e\u4e25\u683c\u9012\u589e\u6392\u5217<\/strong>&nbsp;\u7684\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\u6bcf\u4e2a\u5143\u7d20&nbsp;<strong>\u53ea\u51fa\u73b0\u4e00\u6b21<\/strong>&nbsp;\uff0c\u8fd4\u56de\u5220\u9664\u540e\u6570\u7ec4\u7684\u65b0\u957f\u5ea6\u3002\u5143\u7d20\u7684&nbsp;<strong>\u76f8\u5bf9\u987a\u5e8f<\/strong>&nbsp;\u5e94\u8be5\u4fdd\u6301&nbsp;<strong>\u4e00\u81f4<\/strong>&nbsp;\u3002\u7136\u540e\u8fd4\u56de&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u552f\u4e00\u5143\u7d20\u7684\u4e2a\u6570\u3002<\/p>\n\n\n\n<p>\u8003\u8651&nbsp;<code>nums<\/code>&nbsp;\u7684\u552f\u4e00\u5143\u7d20\u7684\u6570\u91cf\u4e3a&nbsp;<code>k<\/code>&nbsp;\uff0c\u4f60\u9700\u8981\u505a\u4ee5\u4e0b\u4e8b\u60c5\u786e\u4fdd\u4f60\u7684\u9898\u89e3\u53ef\u4ee5\u88ab\u901a\u8fc7\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u66f4\u6539\u6570\u7ec4\u00a0<code>nums<\/code>\u00a0\uff0c\u4f7f\u00a0<code>nums<\/code>\u00a0\u7684\u524d\u00a0<code>k<\/code>\u00a0\u4e2a\u5143\u7d20\u5305\u542b\u552f\u4e00\u5143\u7d20\uff0c\u5e76\u6309\u7167\u5b83\u4eec\u6700\u521d\u5728\u00a0<code>nums<\/code>\u00a0\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u6392\u5217\u3002<code>nums<\/code>\u00a0\u7684\u5176\u4f59\u5143\u7d20\u4e0e\u00a0<code>nums<\/code>\u00a0\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\u3002<\/li>\n\n\n\n<li>\u8fd4\u56de\u00a0<code>k<\/code>\u00a0\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u5224\u9898\u6807\u51c6:<\/strong><\/p>\n\n\n\n<p>\u7cfb\u7edf\u4f1a\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u6765\u6d4b\u8bd5\u4f60\u7684\u9898\u89e3:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int[] nums = [...]; \/\/ \u8f93\u5165\u6570\u7ec4\nint[] expectedNums = [...]; \/\/ \u957f\u5ea6\u6b63\u786e\u7684\u671f\u671b\u7b54\u6848\n\nint k = removeDuplicates(nums); \/\/ \u8c03\u7528\n\nassert k == expectedNums.length;\nfor (int i = 0; i &lt; k; i++) {\n    assert nums[i] == expectedNums[i];\n}<\/pre>\n\n\n\n<p>\u5982\u679c\u6240\u6709\u65ad\u8a00\u90fd\u901a\u8fc7\uff0c\u90a3\u4e48\u60a8\u7684\u9898\u89e3\u5c06\u88ab&nbsp;<strong>\u901a\u8fc7<\/strong>\u3002<\/p>\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,2]\n<strong>\u8f93\u51fa\uff1a<\/strong>2, nums = [1,2,_]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u51fd\u6570\u5e94\u8be5\u8fd4\u56de\u65b0\u7684\u957f\u5ea6 <strong><code>2<\/code><\/strong> \uff0c\u5e76\u4e14\u539f\u6570\u7ec4 <em>nums <\/em>\u7684\u524d\u4e24\u4e2a\u5143\u7d20\u88ab\u4fee\u6539\u4e3a <strong><code>1<\/code><\/strong>, <strong><code>2 <\/code><\/strong><code>\u3002<\/code>\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,2,2,3,3,4]\n<strong>\u8f93\u51fa\uff1a<\/strong>5, nums = [0,1,2,3,4]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u51fd\u6570\u5e94\u8be5\u8fd4\u56de\u65b0\u7684\u957f\u5ea6 <strong><code>5<\/code><\/strong> \uff0c \u5e76\u4e14\u539f\u6570\u7ec4 <em>nums <\/em>\u7684\u524d\u4e94\u4e2a\u5143\u7d20\u88ab\u4fee\u6539\u4e3a <strong><code>0<\/code><\/strong>, <strong><code>1<\/code><\/strong>, <strong><code>2<\/code><\/strong>, <strong><code>3<\/code><\/strong>, <strong><code>4<\/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\u00a0<strong>\u975e\u4e25\u683c\u9012\u589e<\/strong>\u00a0\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);<br>\/\/        return method2(nums, nums.length);<br>        return method3(nums, nums.length);<br>    }<br><br>    <em>\/**<br><\/em><em>     * HashSet -&gt; LinkedHashSet<br><\/em><em>     * 3ms<br><\/em><em>     * <\/em><em>\u590d\u6742\u5ea6<\/em><em>O(n)<br><\/em><em>     *\/<br><\/em><em>    <\/em>public int method1(int[] nums, int n) {<br>        HashSet&lt;Integer&gt; set = new LinkedHashSet&lt;&gt;(n);<br>        for (int num : nums) {<br>            set.add(num);<br>        }<br>        int i = 0;<br>        for (Integer num : set) {<br>            nums[i++] = num;<br>        }<br>        return i;<br>    }<br><br>    <em>\/**<br><\/em><em>     * HashSet + <\/em><em>\u6307\u9488<br><\/em><em>     <\/em><em>* 2ms<br><\/em><em>     * <\/em><em>\u590d\u6742\u5ea6<\/em><em>O(n)<br><\/em><em>     *\/<br><\/em><em>    <\/em>public int method2(int[] nums, int n) {<br>        HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;(n);<br>        int i = 0;<br>        for (int num : nums) {<br>            if (!set.contains(num)) {<br>                set.add(num);<br>                nums[i++] = num;<br>            }<br>        }<br>        return i;<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><em>    <\/em>public int method3(int[] nums, int n) {<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>                j++;<br>            }<br>            \/\/ \u8d70\u5b8c\u6240\u6709\u91cd\u590d\u7684\u503c\uff0c\u8d4b\u503c\u7ed9i<br>            nums[i++] = nums[k];<br>        }<br>        return i;<br>    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>26. \u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879 \u7ed9\u4f60\u4e00\u4e2a&nbsp;\u975e\u4e25\u683c\u9012\u589e\u6392\u5217&nbsp;\u7684\u6570\u7ec4&nbsp;nums&#038;nb [&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-300","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/300","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=300"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/300\/revisions\/301"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}