{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "92e577553712419db2d534bfbd18736a",
  "keywords": "数组,双指针",
  "title": "移除元素",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给你一个数组 <code>nums</code><em>&nbsp;</em>和一个值 <code>val</code>，你需要 <strong><a\nhref=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地</a></strong>\n移除所有数值等于&nbsp;<code>val</code><em>&nbsp;</em>的元素，并返回移除后数组的新长度。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>不要使用额外的数组空间，你必须仅使用 <code>O(1)</code> 额外空间并 <strong><a\nhref=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地 </a>修改输入数组</strong>。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>说明:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>为什么返回数值是整数，但输出的答案是数组呢?</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>请注意，输入数组是以<strong>「引用」</strong>方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>你可以想象内部操作如下:</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre>// <strong>nums</strong> 是以“引用”方式传递的。也就是说，不对实参作任何拷贝\nint len = removeElement(nums, val);",
      "language": "markdown"
    },
    {
      "content": "\n// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\nfor (int i = 0; i &lt; len; i++) {\n&nbsp; &nbsp; print(nums[i]);\n}\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>nums = [3,2,2,3], val = 3\n<strong><br />输出：</strong>2, nums = [2,2]\n<strong><br />解释：</strong>函数应该返回新的长度 <strong>2</strong>, 并且 nums<em> </em>中的前两个元素均为 <strong>2</strong>。你不需要考虑数组中超出新长度后面的元素。例如，函数返回的新长度为 2 ，而 nums = [2,2,3,3] 或 nums = [2,2,0,0]，也会被视作正确答案。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>nums = [0,1,2,2,3,0,4,2], val = 2\n<strong><br />输出：</strong>5, nums = [0,1,4,0,3]\n<strong><br />解释：</strong>函数应该返回新的长度 <strong>5</strong>, 并且 nums 中的前五个元素为 <strong>0</strong>, <strong>1</strong>, <strong>3</strong>, <strong>0</strong>, <strong>4</strong>。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>0 &lt;= nums.length &lt;= 100</code></li>\n<li><code>0 &lt;= nums[i] &lt;= 50</code></li>\n<li><code>0 &lt;= val &lt;= 100</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint removeElement(vector<int> &nums, int val)\n\t{\n\t\tint count = 0;\n\t\tfor (int i = 0; i < nums.size(); i++)\n\t\t{\n\t\t\tif (nums[i] != val)\n\t\t\t{\n\t\t\t\tnums[count++] = nums[i];\n\t\t\t}\n\t\t}\n\t\treturn count;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-93bea2166c9f4473bbebded26b678bb7",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470219"
}