general solution for remove duplicates are allowed at most once/twice/3/...


  • 0
    A
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int ind = 0, step = 1;
            for (auto n: nums) {
                if (ind < step || n != nums[ind - step]) nums[ind++] = n;
            }
            return ind;
        }
    };
    

Log in to reply
 

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