80. 删除有序数组中的重复项 II
方法一
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int len = 0;
for(auto num : nums)
if(len < 2 || nums[len-2] != num)
nums[len++] = num;
return len;
}
};
方法二
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int numsLength = nums.size();
if(numsLength <= 2){
return numsLength;
}
int fast = 2, slow = 2;
while(fast < numsLength){
if(nums[slow - 2] != nums[fast]){
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
};