{"id":298,"date":"2025-06-26T14:48:00","date_gmt":"2025-06-26T06:48:00","guid":{"rendered":"http:\/\/getzs.online\/?p=298"},"modified":"2025-06-30T22:33:10","modified_gmt":"2025-06-30T14:33:10","slug":"%e7%a7%bb%e9%99%a4%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/hello.getzs.online\/index.php\/2025\/06\/26\/%e7%a7%bb%e9%99%a4%e5%85%83%e7%b4%a0\/","title":{"rendered":"\u79fb\u9664\u5143\u7d20"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/remove-element\/\">27. \u79fb\u9664\u5143\u7d20<\/a><\/p>\n\n\n\n<p>\u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4&nbsp;<code>nums<\/code><em>&nbsp;<\/em>\u548c\u4e00\u4e2a\u503c&nbsp;<code>val<\/code>\uff0c\u4f60\u9700\u8981&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<\/a><\/strong>&nbsp;\u79fb\u9664\u6240\u6709\u6570\u503c\u7b49\u4e8e&nbsp;<code>val<\/code><em>&nbsp;<\/em>\u7684\u5143\u7d20\u3002\u5143\u7d20\u7684\u987a\u5e8f\u53ef\u80fd\u53d1\u751f\u6539\u53d8\u3002\u7136\u540e\u8fd4\u56de&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u4e0e&nbsp;<code>val<\/code>&nbsp;\u4e0d\u540c\u7684\u5143\u7d20\u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe&nbsp;<code>nums<\/code>&nbsp;\u4e2d\u4e0d\u7b49\u4e8e&nbsp;<code>val<\/code>&nbsp;\u7684\u5143\u7d20\u6570\u91cf\u4e3a&nbsp;<code>k<\/code>\uff0c\u8981\u901a\u8fc7\u6b64\u9898\uff0c\u60a8\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u66f4\u6539\u00a0<code>nums<\/code>\u00a0\u6570\u7ec4\uff0c\u4f7f\u00a0<code>nums<\/code>\u00a0\u7684\u524d\u00a0<code>k<\/code>\u00a0\u4e2a\u5143\u7d20\u5305\u542b\u4e0d\u7b49\u4e8e\u00a0<code>val<\/code>\u00a0\u7684\u5143\u7d20\u3002<code>nums<\/code>\u00a0\u7684\u5176\u4f59\u5143\u7d20\u548c\u00a0<code>nums<\/code>\u00a0\u7684\u5927\u5c0f\u5e76\u4e0d\u91cd\u8981\u3002<\/li>\n\n\n\n<li>\u8fd4\u56de\u00a0<code>k<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u7528\u6237\u8bc4\u6d4b\uff1a<\/strong><\/p>\n\n\n\n<p>\u8bc4\u6d4b\u673a\u5c06\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6d4b\u8bd5\u60a8\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int[] nums = [...]; \/\/ \u8f93\u5165\u6570\u7ec4\nint val = ...; \/\/ \u8981\u79fb\u9664\u7684\u503c\nint[] expectedNums = [...]; \/\/ \u957f\u5ea6\u6b63\u786e\u7684\u9884\u671f\u7b54\u6848\u3002\n                            \/\/ \u5b83\u4ee5\u4e0d\u7b49\u4e8e val \u7684\u503c\u6392\u5e8f\u3002\n\nint k = removeElement(nums, val); \/\/ \u8c03\u7528\u4f60\u7684\u5b9e\u73b0\n\nassert k == expectedNums.length;\nsort(nums, 0, k); \/\/ \u6392\u5e8f nums \u7684\u524d k \u4e2a\u5143\u7d20\nfor (int i = 0; i &lt; actualLength; i++) {\n    assert nums[i] == expectedNums[i];\n}<\/pre>\n\n\n\n<p>\u5982\u679c\u6240\u6709\u7684\u65ad\u8a00\u90fd\u901a\u8fc7\uff0c\u4f60\u7684\u89e3\u51b3\u65b9\u6848\u5c06\u4f1a&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 = [3,2,2,3], val = 3\n<strong>\u8f93\u51fa\uff1a<\/strong>2, nums = [2,2,_,_]\n<strong>\u89e3\u91ca\uff1a<\/strong>\u4f60\u7684\u51fd\u6570\u51fd\u6570\u5e94\u8be5\u8fd4\u56de k = 2, \u5e76\u4e14 nums<em> <\/em>\u4e2d\u7684\u524d\u4e24\u4e2a\u5143\u7d20\u5747\u4e3a 2\u3002\n\u4f60\u5728\u8fd4\u56de\u7684 k \u4e2a\u5143\u7d20\u4e4b\u5916\u7559\u4e0b\u4e86\u4ec0\u4e48\u5e76\u4e0d\u91cd\u8981\uff08\u56e0\u6b64\u5b83\u4eec\u5e76\u4e0d\u8ba1\u5165\u8bc4\u6d4b\uff09\u3002<\/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>\u4f60\u7684\u51fd\u6570\u5e94\u8be5\u8fd4\u56de k = 5\uff0c\u5e76\u4e14 nums \u4e2d\u7684\u524d\u4e94\u4e2a\u5143\u7d20\u4e3a 0,0,1,3,4\u3002\n\u6ce8\u610f\u8fd9\u4e94\u4e2a\u5143\u7d20\u53ef\u4ee5\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u3002\n\u4f60\u5728\u8fd4\u56de\u7684 k \u4e2a\u5143\u7d20\u4e4b\u5916\u7559\u4e0b\u4e86\u4ec0\u4e48\u5e76\u4e0d\u91cd\u8981\uff08\u56e0\u6b64\u5b83\u4eec\u5e76\u4e0d\u8ba1\u5165\u8bc4\u6d4b\uff09\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>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<pre class=\"wp-block-code\"><code>    public int removeElement(int&#91;] nums, int val) {\n\/\/        return method1(nums, val, nums.length);\n        return method2(nums, val, nums.length);\n    }\n\n    \/**\n     * \u53cc\u6307\u9488\n     * 0ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method1(int&#91;] nums, int val, int n) {\n        int i, j;\n        i = j = n - 1;\n        while (i >= 0) {\n            \/\/ \u5982\u679c\u524d\u6307\u9488nums&#91;i]\u548cval\u76f8\u540c\u5219\u5c06\u540e\u6307\u9488nums&#91;j]\u4e0d\u7b49\u4e8eval\u7684\u8d4b\u503c\u7ed9\u524d\u6307\u9488nums&#91;i]\n            if (nums&#91;i] == val) {\n                \/\/ \u907f\u514dnums&#91;j] == val\n                while (j > i &amp;&amp; nums&#91;j] == val) {\n                    j--;\n                }\n                nums&#91;i] = nums&#91;j--];\n            }\n            i--;\n        }\n        return j + 1;\n    }\n\n    \/**\n     * \u53cc\u6307\u9488\u4f18\u5316\n     * 0ms\n     * \u590d\u6742\u5ea6O(n)\n     *\/\n    public int method2(int&#91;] nums, int val, int n) {\n        int i = 0, j = n - 1;\n        while (i &lt;= j) {\n            \/\/ nums&#91;i] == val \u8868\u793a\u9700\u8981\u5220\u9664\uff0c\u76f4\u63a5\u5c06\u6700\u540e\u4e00\u4e2a\u8d4b\u503c\u7ed9nums&#91;i]\n            if (nums&#91;i] == val) {\n                nums&#91;i] = nums&#91;j];\n                j--;\n            }\n            \/\/ \u4e0d\u540c\u5219\u8df3\u8fc7\n            else {\n                i++;\n            }\n        }\n        return j + 1;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>27. \u79fb\u9664\u5143\u7d20 \u7ed9\u4f60\u4e00\u4e2a\u6570\u7ec4&nbsp;nums&nbsp;\u548c\u4e00\u4e2a\u503c&nbsp;val\uff0c\u4f60\u9700\u8981&nbsp;\u539f [&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-298","post","type-post","status-publish","format-standard","hentry","category-leetcode"],"_links":{"self":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/298","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=298"}],"version-history":[{"count":1,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/posts\/298\/revisions\/299"}],"wp:attachment":[{"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/media?parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/categories?post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hello.getzs.online\/index.php\/wp-json\/wp\/v2\/tags?post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}