文章目录
更多LeetCode题解
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.
My Solution
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size()==0 || nums.size() == 1) return nums.size();
vector<int>::iterator ip = nums.begin();
vector<int>::iterator iq = ip;
while (++iq != nums.end()) {
if (*ip != *iq) {
ip++;
*ip = *iq;
}
}
return ip - nums.begin() + 1;
}
};