16ms c++ solution, very simple


  • 0
    public:
        int removeDuplicates(vector<int>& nums) {
            int len = 1;
            int dup = 1;
            for (int i = 1; i < nums.size(); i++) {
                if (nums[i] == nums[i - 1] && dup < 2) {
                    dup++;
                    len++;
                } else if (nums[i] != nums[i - 1]) {
                    len++;
                    dup = 1;   
                } else {
                    nums.erase(nums.begin() + i);
                    i--;
                }
            }
            return nums.size();
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.