Share my simple 21ms c++ code(with a bool flag controlled)


  • 0
    K
    class Solution {
    
    public:
      int removeDuplicates(int A[], int n) {
            if(n <= 0){
                    return n;
             }
             bool flag = false;
             int pos =0;
             for(int i=1;i<n;i++){
                if(A[pos] != A[i]){
                    A[++pos] = A[i];
                    flag = false;
                }else if(flag == false){
                    A[++pos] = A[i];
                    flag = true;
                }
            }
           return pos+1;
       }
    
    };

Log in to reply
 

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