Java O(n)


  • 0
    M

    public int findMaxConsecutiveOnes(int[] nums) {

        int max = 0;
        int sum = 0;
        for(int i = 0; i < nums.length; i++) {
            
            if(nums[i] == 0) {
                if(sum > max)
                    max = sum;
                sum = 0;
            } else
                sum ++;
        }
    
       // take care from last consecutive ones
        if(sum > 0 && sum > max)
            max = sum;
        return max;
    }

Log in to reply
 

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