Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
题解:求出重复元素的个数,然后就求出了非重复元素的个数 通过 nums[i - count] = nums[i] ,(count代表重复的个数)就修改了数组。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int length = nums.size();
int count = 0;
for (int i = 1; i < length; ++i)
{
if (nums[i] == nums[i - 1])
{
++count;
}
else
{
nums[i - count] = nums[i];
}
}
return length - count;
}
};