Java solution easy to understand


  • 0
    public class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int maxLenWithFlip=0;
            int len=0;
            int lenWithoutFlip=0;
            for(int i=0;i<nums.length;i++)
            {
                if(nums[i]==1) {len++;lenWithoutFlip++;}
                else
                {
                    maxLenWithFlip=Math.max(maxLenWithFlip,len);
                    len=lenWithoutFlip;
                    len++;
                    lenWithoutFlip=0;
                }
            }
            maxLenWithFlip=Math.max(maxLenWithFlip,len);
            return maxLenWithFlip;
        }
    }
    

Log in to reply
 

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