Simple Java Solution Using 2 pointers


  • 0
    H
    public class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        
        int start = 0, max = 0, count = 0;
        for(int i=0;i<nums.length;i++) {
            if(nums[i] == 0) {
                count++;
            }
            
            while(count >= 2) {
                if(nums[start] == 0) {
                    count--;
                }
                start++;
            }
            
            max = Math.max(max, i - start  +1);
        }
        
        return max;
    }
    

    }


Log in to reply
 

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