读题心路历程:一开始读完题的初步想法是想建个哈希表,遍历nums的时候把nums里的数字存进哈希表,然后判断哈希表是否已经有这个数字,有就 +1 ,没有就初始值为0。感觉这个思路也可以,但是有些麻烦,所以还是用双指针的方法去做。
具体代码:
int removeDuplicates(vector<int>& nums) {
int i = 0;
int j = i + 1;
while (j<nums.size())
{
if (nums[i]==nums[j])
{
j++;
}
else
{
nums[i+1]=nums[j];
i++;
}
}
return i+1;
}