Java O(n)t O(1)s


  • 0
    R

    Outside the for loop need add "max=Math.max(count,max)" again to consider some cases;
    eg [0,1,1,1,1] or [1,1,1,1,1]

    public class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            if(nums.length==0) return 0;
            int max=Integer.MIN_VALUE;
            int count=0;
            for(int i=0;i<nums.length;i++){
                if(nums[i]==0) {
                    max=Math.max(count,max);
                    count=0;
                }
                else count++;
            }
            max=Math.max(count,max);
            return max;
        }
    }
    

Log in to reply
 

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