LeetCode 80 Remove Duplicates from Sorted Array II

标签: 数组类题目 LeetCode 发布于:2022-03-26 10:49:33 编辑于:2022-03-26 10:49:55 浏览量:387

概述

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

双指针

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int last = INT_MIN;
        int lastCount = 1;
        int idx = 0;
        for (int i = 0; i < nums.size(); i ++) {
            if (nums[i] != last) {
                last = nums[i];
                lastCount = 1;
                nums[idx++] = nums[i];
            } else {
                lastCount ++;
                if (lastCount <= 2) {
                    nums[idx++] = nums[i];
                }
            }
        }
        return idx;
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/