Given a binary array, count the maximum number of consecutive 1s in it.
The sample1: Input: [1.1.0.1.1.1] output:3Explanation: The first two and the last three are consecutive1, so maximum continuous1The number of is3.
Copy the code
Note:
- The input array contains only zeros and ones.
- The length of the input array is a positive integer and does not exceed 10,000.
The problem solving code
/** * @param {number[]} nums * @return {number} */
var findMaxConsecutiveOnes = function(nums) {
let count = 0;
let i = 0;
let result = [];
while (i < nums.length) {
if (nums[i] === 1) count++;
if(nums[i] ! = =1) count = 0;
result.push(count)
i++;
}
return Math.max(... result) };Copy the code
This article is formatted using MDNICE