Given an ordered array, you need to delete the duplicates in place so that each element appears only once and returns the new length.
Do not define an array separately, you must do this by modifying the input array in place with O (1) extra memory.
Personal code, more mentally retarded.
Class Solution {
Public
int RemoveDuplicates (vector<int>& nums) {
Vector<int>::iterator ITER;
int m;
if (Nums.size () ==0) return 0;
else m=nums[0];
For (Iter=++nums.begin (); Iter!=nums.end ();)
{
if (*iter!=m) {
M=*iter;
++iter;
}
else{
Iter=nums.erase (ITER);
}
}
return int (nums.size ());
}
};
Ranked first code:
The idea is simply to iterate through the array, with different elements in advance, without changing the size of the array.
Class Solution {
Public :
int removeduplicates (vector<int>& nums) { /span>
short int Endpos = 0;
if (nums.size () = = 0) return 0;
for (short int i = 0; I<nums.size (); ++i) {
if (Nums[endpos]!=nums[i]) {
nums[++endpos] = Nums[i];
"
"
return endpos +1;
}
};
Remove duplicates from the sorted array