Any improvement and bug in this code?


  • 0
    Y

    class Solution {
    public:
    int removeDuplicates(int A[], int n) {
    int twice = 0;
    int index = 1;
    if( n <= 1) return n; // ok, could be a joke
    for(int i = 1; i < n; i++){
    if(A[i-1] == A[i] && twice == 1) continue;
    if(A[i-1] == A[i] && twice == 0) twice = 1;
    else twice = 0;
    A[index++] = A[i];
    }
    return index;
    }
    };


Log in to reply
 

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