{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "31c007d8e92d45df854f76b2da1cb73a",
  "keywords": "数组,双指针",
  "title": "删除有序数组中的重复项 II",
  "desc": [
    {
      "content": "\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\"\ntarget=\"_blank\"> 原地</a></strong> 删除重复出现的元素，使每个元素 <strong>最多出现两次</strong> ，返回删除后数组的新长度。</p>\n<p>不要使用额外的数组空间，你必须在 <strong><a href=\"https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95\"\ntarget=\"_blank\">原地 </a>修改输入数组 </strong>并在使用 O(1) 额外空间的条件下完成。</p>\n<p> </p>\n<p><strong>说明：</strong></p>\n<p>为什么返回数值是整数，但输出的答案是数组呢？</p>\n<p>请注意，输入数组是以<strong>「引用」</strong>方式传递的，这意味着在函数里修改输入数组对于调用者是可见的。</p>\n<p>你可以想象内部操作如下:</p>\n<pre>\n// <strong>nums</strong> 是以“引用”方式传递的。也就是说，不对实参做任何拷贝\nint len = removeDuplicates(nums);// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中<strong> 该长度范围内</strong> 的所有元素。\nfor (int i = 0; i < len; i++) {\nprint(nums[i]);\n}</pre>\n<p> </p>\n<p><strong>示例 1：</strong></p>\n<pre><strong>输入：</strong>nums = [1,1,1,2,2,3]<strong><br />输出：</strong>5, nums = [1,1,2,2,3]<strong><br />解释：</strong>函数应返回新长度 length = <strong>5</strong>, 并且原数组的前五个元素被修改为 <strong>1, 1, 2, 2,</strong> <strong>3 </strong>。 不需要考虑数组中超出新长度后面的元素。</pre>\n<p><strong>示例 2：</strong></p>\n<pre><strong>输入：</strong>nums = [0,0,1,1,1,1,2,3,3]<strong><br />输出：</strong>7, nums = [0,0,1,1,2,3,3]<strong><br />解释：</strong>函数应返回新长度 length = <strong>7</strong>, 并且原数组的前五个元素被修改为 <strong>0</strong>, <strong>0</strong>, <strong>1</strong>, <strong>1</strong>, <strong>2</strong>, <strong>3</strong>, <strong>3 。</strong> 不需要考虑数组中超出新长度后面的元素。</pre>\n<p> </p>\n<p><strong>提示：</strong></p>\n<ul>\n<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>\n<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n<li><code>nums</code> 已按升序排列</li>\n</ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\nstatic int removeDuplicates(int *nums, int numsSize)\n{\n\tif (numsSize == 0)\n\t{\n\t\treturn 0;\n\t}\n\tint i;\n\tint len = 0;\n\tint count = 1;\n\tfor (i = 1; i < numsSize; i++)\n\t{\n\t\tif (nums[len] == nums[i])\n\t\t{\n\t\t\tif (count < 2)\n\t\t\t{\n\t\t\t\tcount++;\n\t\t\t\tnums[++len] = nums[i];\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\tcount = 1;\n\t\t\tnums[++len] = nums[i];\n\t\t}\n\t}\n\treturn len + 1;\n}\nint main(int argc, char **argv)\n{\n\tint i, count = argc - 1;\n\tint *nums = malloc(count * sizeof(int));\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tnums[i] = atoi(argv[i + 1]);\n\t}\n\tcount = removeDuplicates(nums, count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%d \", nums[i]);\n\t}\n\tprintf(\"\\n\");\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-f16026ed484a444fb95e3d6698f5f119",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470127"
}