Java Skipping dup '0' method 8s


  • 0
    Z

    For [0,1,1,1,0,0,0,0,1]
    0 1 2 3 4 5 6 7 8
    Skipping from index 4 to 7 by using current max length of consecutive '1'.

    public int findMaxConsecutiveOnes(int[] nums) {
    int max = 0;
    int con = 0;
    int len = nums.length;
    for(int i=0;i<len;i++)
    {
    if(nums[i]==0){
    con = 0;
    }
    else con++;
    if(con>=max){
    max = con;
    continue;
    }
    if(con<=1&&nums[Math.min((max-con+i),len-1)]==0){
    i += max-con;
    con = 0;
    }
    }
    return max;
    }


Log in to reply
 

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