{"id":8064,"date":"2024-04-07T15:09:58","date_gmt":"2024-04-07T07:09:58","guid":{"rendered":"https:\/\/white-night.club\/?p=8064"},"modified":"2024-04-07T15:10:06","modified_gmt":"2024-04-07T07:10:06","slug":"ad176","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2024\/04\/07\/ad176\/","title":{"rendered":"27. \u79fb\u9664\u5143\u7d20"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode.cn\/problems\/remove-element\/description\/\">https:\/\/leetcode.cn\/problems\/remove-element\/description\/<\/a><\/p>\n\n\n\n<p>\u7ecf\u5178\u53cc\u6307\u9488\u3002\u5feb\u6307\u9488\u662f\u4e0d\u505c\u7684\uff0c\u59cb\u7ec8\u79fb\u52a8\u3002\u6162\u6307\u9488\u4f5c\u4e3a\u6700\u540e\u9700\u8981\u8fd4\u56de\u7684\u957f\u5ea6\uff0c\u53ea\u6709\u5728\u5feb\u6307\u9488\u6307\u5411\u4e0d\u9700\u8981\u88ab\u79fb\u9664\u7684\u5143\u7d20\u65f6\uff0c\u624d\u9700\u8981\u79fb\u52a8\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u7b2c2\u4e2a\u5143\u7d20\u9700\u8981\u88ab\u79fb\u9664\uff0c\u90a3\u80af\u5b9a\u662f\u6162\u6307\u9488\u505c\u7559\u5728\u7b2c2\u4e2a\u5143\u7d20\u4e0a\uff0c\u5feb\u6307\u9488\u5411\u540e\u626b\u63cf\uff0c\u76f4\u5230\u626b\u63cf\u5230\u4e0d\u9700\u8981\u79fb\u9664\u7684\u5143\u7d20\uff0c\u518d\u7528\u4e0d\u9700\u8981\u79fb\u9664\u7684\u5143\u7d20\u4e0e\u6162\u6307\u9488\u6307\u5411\u7684\u5143\u7d20\u8fdb\u884c\u66ff\u6362\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">C++<\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution {\npublic:\n    int removeElement(vector&lt;int&gt;&amp; nums, int val) {\n        int left=0;\n        for(int right=0;right&lt;nums.size();right++)\n            if(nums[right]!=val)\n                nums[left++]=nums[right];\n        return left;\n    }\n};\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">removeElement<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #4EC9B0\">vector<\/span><span style=\"color: #D4D4D4\">&lt;<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\">&gt;<\/span><span style=\"color: #569CD6\">&amp;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">val<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> left=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> right=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">;right&lt;<\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">size<\/span><span style=\"color: #D4D4D4\">();right++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">[right]!=val)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">[left++]=<\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">[right];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> left;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">};<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u800c\u4e14\u6211\u7a81\u7136\u53d1\u73b0\u8fd9\u4e2acode\u533a\u5757\u4f1a\u628aC++\u91cc\u9762\u7684\u201c!=\u201d\u8bc6\u522b\u6210\u201c\u2260\u201d\uff0c\u4e0d\u8fc7\u95ee\u9898\u4e5f\u4e0d\u5927\uff0c\u81ea\u5df1\u80fd\u770b\u61c2\u5c31\u884c\u4e86\u3002<\/p>\n\n\n\n<p>\u987a\u4fbf\u7528Go\u5199\u4e0b\u4e86\u3002go\u4e0d\u80fd\u5199\u6210left++\u8fd8\u662f\u6709\u70b9\u9ebb\u70e6\u7684\u3002<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7\">Go<\/span><span role=\"button\" tabindex=\"0\" data-code=\"func removeElement(nums []int, val int) int {\n    left:=0\n    for right:=0;right&lt;len(nums);right++{\n        if nums[right]!=val{\n            nums[left]=nums[right]\n            left++\n        }\n    }\n    return left\n}\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">func<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">removeElement<\/span><span style=\"color: #D4D4D4\">(nums []<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">, val <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #9CDCFE\">left<\/span><span style=\"color: #D4D4D4\">:=<\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">right<\/span><span style=\"color: #D4D4D4\">:=<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">;right&lt;<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums);right++{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[right]!=val{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            nums[left]=nums[right]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            left++<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> left<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">}<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u53cc\u6307\u9488\u5165\u95e8<\/p>\n","protected":false},"author":1,"featured_media":7932,"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\/8064"}],"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=8064"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/8064\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/7932"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=8064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=8064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=8064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}