{"id":2030,"date":"2023-09-28T10:30:26","date_gmt":"2023-09-28T02:30:26","guid":{"rendered":"https:\/\/white-night.club\/?p=2030"},"modified":"2023-11-09T11:07:52","modified_gmt":"2023-11-09T03:07:52","slug":"ad18","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/09\/28\/ad18\/","title":{"rendered":"26.\u00a0\u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u00a0<strong>\u975e\u4e25\u683c\u9012\u589e\u6392\u5217<\/strong>\u00a0\u7684\u6570\u7ec4\u00a0<code>nums<\/code>\u00a0\uff0c\u8bf7\u4f60<strong><a rel=\"noreferrer noopener\" href=\"http:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">\u00a0<mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">\u539f\u5730<\/mark><\/a><\/strong>\u00a0\u5220\u9664\u91cd\u590d\u51fa\u73b0\u7684\u5143\u7d20\uff0c\u4f7f\u6bcf\u4e2a\u5143\u7d20\u00a0<strong>\u53ea\u51fa\u73b0\u4e00\u6b21<\/strong>\u00a0\uff0c\u8fd4\u56de\u5220\u9664\u540e\u6570\u7ec4\u7684\u65b0\u957f\u5ea6\u3002\u5143\u7d20\u7684\u00a0<strong>\u76f8\u5bf9\u987a\u5e8f<\/strong>\u00a0\u5e94\u8be5\u4fdd\u6301\u00a0<strong>\u4e00\u81f4<\/strong>\u00a0\u3002\u7136\u540e\u8fd4\u56de\u00a0<code>nums<\/code>\u00a0\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>\n<li>\u66f4\u6539\u6570\u7ec4&nbsp;<code>nums<\/code>&nbsp;\uff0c\u4f7f&nbsp;<code>nums<\/code>&nbsp;\u7684\u524d&nbsp;<code>k<\/code>&nbsp;\u4e2a\u5143\u7d20\u5305\u542b\u552f\u4e00\u5143\u7d20\uff0c\u5e76\u6309\u7167\u5b83\u4eec\u6700\u521d\u5728&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u51fa\u73b0\u7684\u987a\u5e8f\u6392\u5217\u3002<code>nums<\/code>&nbsp;\u7684\u5176\u4f59\u5143\u7d20\u4e0e&nbsp;<code>nums<\/code>&nbsp;\u7684\u5927\u5c0f\u4e0d\u91cd\u8981\u3002<\/li>\n\n\n\n<li>\u8fd4\u56de&nbsp;<code>k<\/code>&nbsp;\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>\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>&nbsp;\u5df2\u6309&nbsp;<strong>\u975e\u4e25\u683c\u9012\u589e<\/strong>&nbsp;\u6392\u5217<\/li>\n<\/ul>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u771f\u7684\u8981\u201c\u5220\u9664\u201d\u5417\uff1f<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u4e00\u5f00\u59cb\u4ee5\u4e3a\u771f\u7684\u662f\u8981\u5220\u9664\uff0c\u6240\u4ee5\u7528\u4e86vector\u81ea\u5e26\u7684erase\u53bb\u5220\u9664\u5143\u7d20\u3002\u672c\u6765\u8fd8\u60f3\u7528\u8fed\u4ee3\u5668\uff0c\u4f46\u662f\u53d1\u73b0\u6ca1\u5fc5\u8981\u5c31\u6ca1\u7528\u4e86\u3002\u601d\u8def\u7684\u8bdd&#8230;\u4e5f\u6ca1\u5565\u601d\u8def\uff0cerase\u5220\u5b8c\u5143\u7d20\u540e\u4f1a\u81ea\u52a8\u5e2e\u4f60\u628a\u540e\u9762\u7684\u5143\u7d20\u5f80\u524d\u6392\u3002\u4f46\u662f\u8fd9\u5c31\u6709\u4e2a\u95ee\u9898\uff0c\u5f85\u4f1a\u63d0\u3002<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>\u4ee3\u7801\uff08\u4f7f\u7528erase\uff09<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int removeDuplicates(vector&lt;int&gt;&amp; nums) {\n       int length=nums.size();\n       for(int i=1;i&lt;length;)\n       {\n           if(nums&#91;i-1]==nums&#91;i])\n           {\n               nums.erase(nums.begin()+i);\n               length--;\n           }else\n           {\n               i++;\n           }\n       }\n       return length;\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e48\u505a\u7684\u540e\u679c\u5c31\u662f\uff0c\u6548\u7387\u592a\u4f4e\u3002\u5c31\u7b97\u7528<em>nums.shrink_to_fit();<\/em>\u53bb\u91ca\u653e\u5185\u5b58\uff08erase\u53ea\u4f1a\u5220\u9664vector\u4e2d\u7684\u5143\u7d20\uff0c\u4e0d\u4f1a\u91ca\u653e\u8be5\u5143\u7d20\u5360\u7528\u7684\u5185\u5b58\uff09\u4e5f\u6ca1\u4ec0\u4e48\u6548\u679c\uff0c\u8017\u65f6\u4ecd\u7136\u8fbe\u5230480ms+\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u770b\u4e86\u770b\u5b98\u65b9\u9898\u89e3\uff0c\u53d1\u73b0\u5e76\u4e0d\u662f\u8981\u771f\u7684\u5220\u9664\u67d0\u4e2a\u5143\u7d20&#8230;.\u5b9e\u9645\u4e0a\u662f\u628a\u91cd\u590d\u7684\u5143\u7d20\u5f80\u540e\u6392\uff0c\u4e0d\u91cd\u590d\u7684\u5143\u7d20\u5f80\u524d\u6392\u3002\u4e3e\u4e2a\u4f8b\u5b50\u3002<\/p>\n\n\n\n<ul>\n<li>\u521d\u59cb\uff1a[0,0,1,1,1,2,2,3,3,4]<\/li>\n\n\n\n<li>\u201c\u5220\u9664\u201d\uff1a[<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">0,1,2,3,4<\/mark>,2,2,3,3,4]<\/li>\n<\/ul>\n\n\n\n<p>\u771f\u7684\u5220\u4e86\u5143\u7d20\u5417\uff1f\u5982\u5220\u3002\u5feb\u6162\u6307\u9488\u7684\u8bdd\u4e5f\u4e0d\u96be\u7406\u89e3\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u6bd5\u7adf\u8fd8\u662fO(n)\u3002\u80fd\u4e0d\u80fd\u770b\u51fa\u6765\u9898\u76ee\u8981\u6c42\u662f\u201c\u66ff\u6362\u201d\u800c\u4e0d\u662f\u201c\u5220\u9664\u201d\u624d\u662f\u95ee\u9898\uff08\u6211\u53cd\u6b63\u662f\u6ca1\u770b\u51fa\u6765\ud83d\udc12\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int removeDuplicates(vector&lt;int&gt;&amp; nums) {\n        int n = nums.size();\n        if (n == 0) {\n            return 0;\n        }\n        int fast = 1, slow = 1;\n        while (fast &lt; n) {\n            if (nums&#91;fast] != nums&#91;fast - 1]) {\n                nums&#91;slow] = nums&#91;fast];\n                ++slow;\n            }\n            ++fast;\n        }\n        return slow;\n    }\n};\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1avector.erase&#038;&#038;\u53cc\u6307\u9488<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2030"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=2030"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2030\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=2030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=2030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=2030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}