Share my simple java solution


  • 0
    X
    public int findMaxConsecutiveOnes(int[] nums) {
            int max = 0, i = 0;
            while (i < nums.length) {
                if (nums[i] == 1) {
                    int j = i;
                    while (j < nums.length && nums[j] == 1) j++;
                    max = Math.max(j - i, max);
                    i = j;
                } else {
                    i++;
                }
            } 
            return max;
        }
    
    

Log in to reply
 

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