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;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20