LeetCode 485. Max Consecutive Ones

解:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.

求出数组中连续1的最大的个数

let count = 0; // 记录连续+1的值
    let max = 0;// 记录最大的值
    for(let i = 0; i < nums.length; i++){
        if(nums[i] == 1)  count++;
        if(max < count)   max=count;
        if(nums[i] == 0)   count=0;
    }
    return max

  

猜你喜欢

转载自www.cnblogs.com/xiyu-8023/p/9273453.html