C++ O(n)


  • 0
    J
        int findMaxConsecutiveOnes(vector<int>& nums) {
            int maxLen = 0;
            int b1 = -1;
            int b2 = b1;
            for (int i = 0; i < nums.size(); ++i) {
                if (nums[i] == 0) {
                    maxLen = max(maxLen, i - b1 - 1);
                    b1 = b2;
                    b2 = i;
                }
            }
            
            return max(maxLen, int(nums.size() - b1 - 1));
        }
    

Log in to reply
 

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