Easy-understand JAVA solution beats 94.3%


  • 0
    Z
    public int findMaxConsecutiveOnes(int[] nums) {
            int count = 0, res = 0, i = 0;
            while(i<nums.length&&nums[i]==1){count++;i++;}
            //no flip
            if(i>=nums.length)return count;
            int prev = count;
            while(i<nums.length){
                count = 0;
                prev+=1;//flip one
                i++;
                while(i<nums.length&&nums[i]==1){count++;i++;}
                int cur = count;
                res = Math.max(res,cur+prev);
                prev = cur;
            }
            return res;
        }
    

Log in to reply
 

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