O(n) C++ solution


  • 0
    R
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int nums_size = nums.size();
        int max_ones = 0, cur_max  = 0;
    
        for(int i=0; i<size; i++) {
            cur_max = 0;
            while (nums[i++] == 1) cur_max++;
            max_ones = max(max_ones, cur_max);
        }
        return max_ones;
    }
    

Log in to reply
 

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