{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "521d27470d934267b7faf7d0cc2212c0", "keywords": "数组,双指针", "title": "删除有序数组中的重复项", "desc": [ { "content": "\n
\n

给你一个有序数组 nums ,请你\n原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。

", "language": "markdown" }, { "content": "\n

不要使用额外的数组空间,你必须在 原地\n修改输入数组 并在使用 O(1) 额外空间的条件下完成。

", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

说明:

", "language": "markdown" }, { "content": "\n

为什么返回数值是整数,但输出的答案是数组呢?

", "language": "markdown" }, { "content": "\n

请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

", "language": "markdown" }, { "content": "\n

你可以想象内部操作如下:

", "language": "markdown" }, { "content": "\n
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝\nint len = removeDuplicates(nums);",
      "language": "markdown"
    },
    {
      "content": "\n// 在函数里修改输入数组对于调用者是可见的。\n// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。\nfor (int i = 0; i < len; i++) {\n    print(nums[i]);\n}\n
\n ", "language": "markdown" }, { "content": "\n

示例 1:

", "language": "markdown" }, { "content": "\n
输入:nums = [1,1,2]\n
输出:
2, nums = [1,2]\n
解释:
函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。\n
", "language": "markdown" }, { "content": "\n

示例 2:

", "language": "markdown" }, { "content": "\n
输入:nums = [0,0,1,1,1,2,2,3,3,4]\n
输出:
5, nums = [0,1,2,3,4]\n
解释:
函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。\n
", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

提示:

", "language": "markdown" }, { "content": "\n", "language": "markdown" }, { "content": "\n

 

\n
", "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" }