java solution with for and while


  • 0
    M

    public int findMaxConsecutiveOnes(int[] nums) {
    if(nums == null || nums.length == 0) return 0;
    //int start = 0;
    int max = 0;
    for(int i = 0; i < nums.length; i++) {
    if(nums[i] == 1) {
    // find the consecutive 1
    int j = i;
    int len = 0;
    while(j < nums.length && nums[j] != 0) {
    len++;
    j++;
    }
    max = Math.max(max, len);
    }
    }
    return max;
    }


Log in to reply
 

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