{"id":2450,"date":"2023-10-13T15:27:16","date_gmt":"2023-10-13T07:27:16","guid":{"rendered":"https:\/\/white-night.club\/?p=2450"},"modified":"2023-11-09T11:07:50","modified_gmt":"2023-11-09T03:07:50","slug":"ad45","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/10\/13\/ad45\/","title":{"rendered":"27.\u00a0\u79fb\u9664\u5143\u7d20"},"content":{"rendered":"\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4\u00a0<code>nums<\/code><em>\u00a0<\/em>\u548c\u4e00\u4e2a\u503c\u00a0<code>val<\/code>\uff0c\u4f60\u9700\u8981\u00a0<strong><a rel=\"noreferrer noopener\" href=\"https:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">\u539f\u5730<\/mark><\/a><\/strong>\u00a0\u79fb\u9664\u6240\u6709\u6570\u503c\u7b49\u4e8e\u00a0<code>val<\/code><em>\u00a0<\/em>\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u79fb\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\u4ec5\u4f7f\u7528\u00a0<code>O(1)<\/code>\u00a0\u989d\u5916\u7a7a\u95f4\u5e76<mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">\u00a0<\/mark><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\"><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\">\u539f\u5730<\/mark><\/a><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0086ff\" class=\"has-inline-color\"><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/baike.baidu.com\/item\/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\" target=\"_blank\">\u00a0<\/a><\/strong><\/mark><strong>\u4fee\u6539\u8f93\u5165\u6570\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<p>\u5143\u7d20\u7684\u987a\u5e8f\u53ef\u4ee5\u6539\u53d8\u3002\u4f60\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>\u8bf4\u660e:<\/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?<\/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\u4f5c\u4efb\u4f55\u62f7\u8d1d\nint len = removeElement(nums, val);\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 = [3,2,2,3], val = 3\n<strong>\u8f93\u51fa\uff1a<\/strong>2, nums = [2,2]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u51fd\u6570\u5e94\u8be5\u8fd4\u56de\u65b0\u7684\u957f\u5ea6 <strong>2<\/strong>, \u5e76\u4e14 nums<em> <\/em>\u4e2d\u7684\u524d\u4e24\u4e2a\u5143\u7d20\u5747\u4e3a <strong>2<\/strong>\u3002\u4f60\u4e0d\u9700\u8981\u8003\u8651\u6570\u7ec4\u4e2d\u8d85\u51fa\u65b0\u957f\u5ea6\u540e\u9762\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u51fd\u6570\u8fd4\u56de\u7684\u65b0\u957f\u5ea6\u4e3a 2 \uff0c\u800c nums = [2,2,3,3] \u6216 nums = [2,2,0,0]\uff0c\u4e5f\u4f1a\u88ab\u89c6\u4f5c\u6b63\u786e\u7b54\u6848\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,1,2,2,3,0,4,2], val = 2\n<strong>\u8f93\u51fa\uff1a<\/strong>5, nums = [0,1,4,0,3]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u51fd\u6570\u5e94\u8be5\u8fd4\u56de\u65b0\u7684\u957f\u5ea6 <strong><code>5<\/code><\/strong>, \u5e76\u4e14 nums \u4e2d\u7684\u524d\u4e94\u4e2a\u5143\u7d20\u4e3a <strong><code>0<\/code><\/strong>, <strong><code>1<\/code><\/strong>, <strong><code>3<\/code><\/strong>, <strong><code>0<\/code><\/strong>, <strong>4<\/strong>\u3002\u6ce8\u610f\u8fd9\u4e94\u4e2a\u5143\u7d20\u53ef\u4e3a\u4efb\u610f\u987a\u5e8f\u3002\u4f60\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>0 &lt;= nums.length &lt;= 100<\/code><\/li>\n\n\n\n<li><code>0 &lt;= nums[i] &lt;= 50<\/code><\/li>\n\n\n\n<li><code>0 &lt;= val &lt;= 100<\/code><\/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>\u53cc\u6307\u9488<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5f00\u59cb\u5237\u4e9b\u53cc\u6307\u9488\u7684\u9898\uff0c\u540e\u9762\u518d\u5237\u4e9b\u9012\u5f52\u548c\u52a8\u6001\u89c4\u5212\u7684\u9898\u3002<\/p>\n\n\n\n<p>\u5feb\u6307\u9488\u4e00\u8def\u5f80\u540e\u904d\u5386\uff0c\u5982\u679c\u7b49\u4e8eval\u5c31\u8df3\u8fc7\uff0c\u4e00\u76f4\u5230\u4e0d\u7b49\u4e8eval\u4e3a\u6b62\u3002\u6b64\u65f6\u518d\u5c06\u5feb\u6307\u9488\u6307\u5411\u7684\u503c\u66ff\u6362\u6162\u6307\u9488\u6307\u5411\u7684\u503c\uff0c\u5e76\u5c06\u6162\u6307\u9488\u5f80\u540e\u79fb\u4e00\u4f4d\u3002\u6700\u540e\u6162\u6307\u9488\u7684\u503c\u5c31\u662f\u6700\u540e\u94fe\u8868\u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\r\npublic:\r\n    int removeElement(vector&lt;int>&amp; nums, int val) {\r\n        int len=nums.size();\r\n        if(len==0)\r\n        {\r\n            return len;\r\n        }\r\n        int left=0,right=0;\r\n        for(;right&lt;len;right++)\r\n        {\r\n            if(nums&#91;right]!=val)\r\n            {\r\n                nums&#91;left]=nums&#91;right];\r\n                left++;\r\n            }\r\n        }\r\n        return left;\r\n    }\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":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\/2450"}],"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=2450"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2450\/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=2450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=2450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=2450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}