Java 10 line with O(N)


  • 0
    A
    public int findMaxConsecutiveOnes(int[] nums) {
        int pos = -1, res = 0, runningSum = 0;
        for(int i=0; i<nums.length;i++){
            if(nums[i]==0){
                runningSum = i-pos;
                pos = i;
            }
            else runningSum++;
            res = Math.max(res, runningSum);
        }
        return res;        
    }

Log in to reply
 

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