{"id":5302,"date":"2023-11-18T14:33:42","date_gmt":"2023-11-18T06:33:42","guid":{"rendered":"https:\/\/white-night.club\/?p=5302"},"modified":"2023-11-18T15:53:24","modified_gmt":"2023-11-18T07:53:24","slug":"ad156","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/11\/18\/ad156\/","title":{"rendered":"80.\u00a0\u5220\u9664\u6709\u5e8f\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u9879 II"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6709\u5e8f\u6570\u7ec4\u00a0<code>nums<\/code>\u00a0\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\">\u00a0\u539f\u5730<\/a><\/strong>\u00a0\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>\u00a0\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:\/\/ <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 int len = removeDuplicates(nums); \/\/ \u5728\u51fd\u6570\u91cc\u4fee\u6539\u8f93\u5165\u6570\u7ec4\u5bf9\u4e8e\u8c03\u7528\u8005\u662f\u53ef\u89c1\u7684\u3002 \/\/ \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 for (int i = 0; i &lt; len; i++) { &nbsp; &nbsp; print(nums[i]); }<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><strong>\u8f93\u5165\uff1a<\/strong>nums = [1,1,1,2,2,3] <strong>\u8f93\u51fa\uff1a<\/strong>5, nums = [1,1,2,2,3] <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<\/p>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><strong>\u8f93\u5165\uff1a<\/strong>nums = [0,0,1,1,1,1,2,3,3] <strong>\u8f93\u51fa\uff1a<\/strong>7, nums = [0,0,1,1,2,3,3] <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\u4e94\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<\/p>\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\u5347\u5e8f\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 has-link-color wp-elements-db7d1dcdabd125fe3cb1cd106e289972\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>\u53cc\u6307\u9488<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u672c\u6765\u8fd8\u60f3\u7528\u5feb\u6307\u9488\u53bb\u6bd4\u8f83\u7684\uff0c\u4f46\u662f\u53d1\u73b0\u7b2c\u4e00\u4e2a\u6837\u4f8b\u5c31\u8fc7\u4e0d\u4e86\u3002<\/p>\n\n\n\n<p>\u800c\u4e14\u7a81\u7136\u53d1\u73b0\uff0c\u4fdd\u7559k\u4e2a\u91cd\u590d\u5143\u7d20\uff0c\u90a3\u76f4\u63a5\u7528right\u2260left-k\u53bb\u6bd4\u8f83\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u8fd9\u601d\u8def\u6709\u70b9\u50cf\uff1a\u4eceleft-k\u7684\u4f4d\u7f6e\u5f00\u59cb\u6bd4\u8f83\uff0c\u5982\u679c\u91cd\u590d\u5c31\u79fb\u52a8right\u76f4\u5230right!=left-k\uff0c\u7136\u540e\u66ff\u6362\u6389left-k\u518dleft++\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u4fdd\u75593\u4e2a\u91cd\u590d\u7684<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>1 1 1 1 2 2 2 3<\/strong><\/p>\n\n\n\n<p>\u7eb8\u8d28\u8fc7\u7a0b\u5982\u56fe\uff0c\u8bf4\u5b9e\u8bdd\u53cc\u6307\u9488\u771f\u7684\u6709\u70b9\u62bd\u8c61\uff0c\u8bf6\u3002<\/p>\n\n\n\n<p>\u8fd4\u56de\u7684\u662fleft\uff0c\u8fd9\u4e2aleft\u5b9e\u9645\u4e0a\u662f\u5143\u7d20\u4e2a\u6570\u3002\u6240\u4ee5\u5b9e\u9645\u4e0a\u8fd4\u56de\u7684\u4e0d\u662f\u4ecenums[0]\u5230nums[left]\u7684\u5143\u7d20\uff0c\u800c\u662f\u5230nums[left-1]\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large has-custom-border\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"1024\" src=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/b0c83ca41286b1f73b30ca133214519-618x1024.png\" alt=\"\" class=\"wp-image-5309\" style=\"border-style:none;border-width:0px;border-radius:0px\" srcset=\"https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/b0c83ca41286b1f73b30ca133214519-618x1024.png 618w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/b0c83ca41286b1f73b30ca133214519-181x300.png 181w, https:\/\/white-night.club\/wp-content\/uploads\/2023\/11\/b0c83ca41286b1f73b30ca133214519.png 734w\" sizes=\"(max-width: 618px) 100vw, 618px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>func removeDuplicates(nums &#91;]int) int {\r\n    size:=len(nums)\r\n    if size&lt;=2{\r\n        return size\r\n    }\r\n    left,right:=2,2\r\n    for right&lt;size{\r\n        if nums&#91;left-2]!=nums&#91;right]{\r\n            nums&#91;left]=nums&#91;right]\r\n            left++\r\n        }\r\n        right++\r\n    }\r\n    return left\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1a\u53cc\u6307\u9488<\/p>\n","protected":false},"author":1,"featured_media":5306,"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\/5302"}],"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=5302"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/5302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/5306"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=5302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=5302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=5302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}