Easy Java solution


  • 0
    P
    class Solution {
        public int findMaxConsecutiveOnes(int[] nums) {
            int max = Integer.MIN_VALUE;
            
            for (int i=0;i<nums.length;){
                int start = i;
                if (nums[i]==1){                
                    int size = 0;
                    while (start<nums.length&&nums[start]==1){
                        size++;
                        start++;                                        
                    }                
                    if (size>max){max=size;}
                }
                i=start+1;            
            }
            
            return (max==Integer.MIN_VALUE) ? 0 : max;
        }
    }
    

Log in to reply
 

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