8ms Java O(n) solution


  • 0
    R
    public class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int len = nums.length;
            int prevIndex = -1;
            int maxLength = Integer.MIN_VALUE;
            for (int i = 0; i < len; i++) {
                if (nums[i] == 0) {
                    int diff = i - prevIndex -1;
                    maxLength = Math.max(maxLength, diff);
                    prevIndex = i;
                }
            }
            maxLength = Math.max(maxLength, len - prevIndex - 1);
            return maxLength;
        }
    }
    

Log in to reply
 

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