My O(n) C++ solution


  • -1
    G
    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            if(n < 3) return n;
            int count(1), i(0), dup(0);
            while(++i < n){
                if(A[i] == A[i-1]) dup++;
                else dup = 0;
                if(dup < 2 ) A[count++] = A[i];
            }
            return count;
        }
    };

Log in to reply
 

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