1. Problem Description
Given a sorted array, 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 in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
就地有序数组去重!
2. My solution
两个指针,这个题和前面的27. Remove Element 思路一样。
int removeDuplicates(vector<int>& nums) {
int ansidx=0,numidx=0;
int len=nums.size();
int pre;
for(numidx=0;numidx<len;numidx++)
{
if(numidx==0)
{
nums[ansidx++]=nums[numidx];
pre=nums[0];
continue;
}
if(nums[numidx]!=pre)
{
nums[ansidx++]=nums[numidx];
pre=nums[numidx];
}
}
return ansidx;
}