80. Remove Duplicates from Sorted Array II 發表於 2023-02-13 | 分類於 leetcode problemsolution1234567891011121314151617181920212223class Solution {public: int removeDuplicates(vector<int>& nums) { int n= nums.size(); int slow = 0, fast = 1, count =1; while(fast<n){ if(nums[fast] == nums[slow] && count==1){ count ++; nums[++slow] = nums[fast]; fast++; } else if(nums[fast] == nums[slow] && count==2) fast++; else if(nums[fast]!=nums[slow]){ nums[++slow] = nums[fast]; count = 1; fast++; } } return slow+1; }}; analysis time complexity O(n) space complexity O(1)