Description: Given a sorted array, delete the repeating number in the original array so that each element appears only once and returns the length of the new array.
Do not use additional array space and must be done without additional space in place.
Sample Example
to the array A =[1,1,2], your function should return a length of 2, at this time a=[1,2].
Ideas:
The first is wrong, and then compiled through, this problem my idea is: first, because the problem is not required to add the array space, so we consider in the original array of coverage, that is, the repetition of the number of the front of a number, followed by the next step forward, in the programming, there is no delete said, just the data coverage. After overwriting the previous, the last can be done by narrowing the data space.
Code:
Class Solution {
Public
int RemoveDuplicates (vector<int> &nums) {
if (Nums.empty ())
{
return 0;
}
int n = nums.size (), k=0;
for (int i=1;i<n;++i)
{
if (nums[i]! = Nums[k])
{
NUMS[++K] = nums[i];
}
}
Nums.resize (k+1);
return k+1;
}
};
Delete a repeating number in an ordered array