Easy to understand Java solution


  • 0

    Use a boolean flag to record if we have used the flip yet or not.

    public int findMaxConsecutiveOnes(int[] nums) {
            boolean flipused=false;
            int res=0;
            int tmp=0;
            int half=0;
            for(int a:nums){
                if(a==1){
                    tmp++;
                    if(flipused) half++;
                }else{
                    if(flipused){
                        res=Math.max(res, tmp);
                        tmp=half;
                        half=0;
                        tmp++;
                    }else{
                        flipused=true;
                        tmp++;
                    }
                }
            }
            res=Math.max(res, tmp);
            return res;
        }
    

Log in to reply
 

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