Nice java solution and easy to understand


  • 0
    L

    Need to define two length variables for flip and non-flip case.
    If current value is 1, add both length by 1.
    If current value is 0, length with flip should be previous length without flip plus one, and reset length without flipt as 0.
    Here is the code.

    class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int max=0;
            int len=0; //length without flip
            int lenFlip=0; //length with flip
            for(int v:nums){
                if(v==1){
                    len++;
                    lenFlip++;
                } else{
                    lenFlip=len+1;
                    len=0;
                }
                max=Math.max(max,lenFlip);
            }
            return max;
        }
    }
    

Log in to reply
 

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