{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "521d27470d934267b7faf7d0cc2212c0",
  "keywords": "数组,双指针",
  "title": "删除有序数组中的重复项",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给你一个有序数组 <code>nums</code> ，请你<strong><a href=\"http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">\n原地</a></strong> 删除重复出现的元素，使每个元素 <strong>只出现一次</strong> ，返回删除后数组的新长度。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>不要使用额外的数组空间，你必须在 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\">原地\n</a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</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 = removeDuplicates(nums);",
      "language": "markdown"
    },
    {
      "content": "\n// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\nfor (int i = 0; i &lt; len; i++) {\n&nbsp; &nbsp; print(nums[i]);\n}\n</pre>\n&nbsp;",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>nums = [1,1,2]\n<strong><br />输出：</strong>2, nums = [1,2]\n<strong><br />解释：</strong>函数应该返回新的长度 <strong>2</strong> ，并且原数组 <em>nums </em>的前两个元素被修改为 <strong>1</strong>, <strong>2 </strong>。不需要考虑数组中超出新长度后面的元素。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>nums = [0,0,1,1,1,2,2,3,3,4]\n<strong><br />输出：</strong>5, nums = [0,1,2,3,4]\n<strong><br />解释：</strong>函数应该返回新的长度 <strong>5</strong> ， 并且原数组 <em>nums </em>的前五个元素被修改为 <strong>0</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</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;= 3 * 10<sup>4</sup></code></li>\n<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n<li><code>nums</code> 已按升序排列</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic int removeDuplicates(int[] nums) {\n\t\tif (nums == null || nums.length == 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tint a = 0;\n\t\tfor (int b = 1; b < nums.length; b++) {\n\t\t\tif (nums[a] != nums[b]) {\n\t\t\t\ta++;\n\t\t\t\tnums[a] = nums[b];\n\t\t\t}\n\t\t}\n\t\treturn a + 1;\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-2ab0d6770e5f46889d71ba2f4b2e1fb1",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600470118"
}