Given a sorted array, remove the duplicates in place such that each element appear only once andreturnthe new length.
Do not allocate extra spacefor another array, you must do this in place withconstant memory.
For example,
Given input array nums = [1,1,2],
Your function should returnlength = 2, withthefirst two elements of nums being 1and2 respectively.
It doesn't matter what you leave beyond the new length.
代码
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.begin() == nums.end()) return0;
vector<int>::iterator itor;
for (itor = nums.begin(); itor != nums.end() && itor + 1 != nums.end(); ++itor) {
while (*itor == *(itor + 1)) {
nums.erase(itor + 1);
if (itor + 1 == nums.end())
break;
}
}
return nums.size();
}
};