题目描述
题目分析
又是一道贴错标签的简单题,很明显的双指针,我的做法是用两个变量保存是否需要记录,官方题解的做法是直接判断,人家的高明一些
class Solution {
public:int removeDuplicates(vector<int>& nums) {int n = nums.size();if (n < 3) return n;int i = 0, j = 0, cnt = 0, now = INT_MIN;for (; j < n; ++j) {if (now != nums[j]) {now = nums[j];cnt = 1;nums[i++] = nums[j];} else {if (cnt < 2) {++cnt;nums[i++] = nums[j];} else {continue;}}}return i;}
};