This is leetcode 26. Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. An example is given nums = [1,1,2], the function should return [1,2].
Below is my code. I delete all the other duplicates, just leave one of them. However I always got an error of reference binding to null pointer of type 'value_type' when submitting. I would appreciate if anyone can help me with this!
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = 0;
while(i < nums.size() - 1) {
if (nums[i] == nums[i + 1]) {
nums.erase(nums.begin() + i);
}
else i++;
}
return nums.size();
}
};