C++ 2 pointers O(n) solution


  • 0
        int findMaxConsecutiveOnes(vector<int>& nums) {
            int max=0;
            vector<int>::iterator head=nums.begin();
            vector<int>::iterator tail=nums.begin();
            while(tail!=nums.end()){
                while(*tail==1) tail++;
                if(tail-head>max) max=tail-head;
                head=tail;
                while(*head==0&&head!=nums.end()) head++;
                tail=head;
            }
            return max;
        }
    

Log in to reply
 

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