/*****************************************************问题描述*************************************************
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
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.
Note:
The input array will only contain 0 and 1.
The length of input array is a positive integer and will not exceed 10,000
给定一个整数数组,只包含0和1,找到最长的连续1的个数
/*****************************************************我的解答*************************************************
/**
* @param {number[]} nums
* @return {number}
*/
var findMaxConsecutiveOnes = function(nums) {
var highestNumber = 0;
var currentNum = 0;
var currentHigh = 0;
for(var index = 0; index < nums.length; index++)
{
if(nums[index] === 0)
{
currentNum = 0;
continue;
}
else
{
if(currentNum === 0)
{
currentHigh = 1;
currentNum = 1;
}
else
{
currentHigh++;
}
}
if(currentHigh > highestNumber)
{
highestNumber = currentHigh;
}
}
return highestNumber;
};
console.log(findMaxConsecutiveOnes([1,1,0,1,1,1]));
leetCode刷题记录60_485_Max Consecutive Ones
猜你喜欢
转载自blog.csdn.net/gunsmoke/article/details/88991816
今日推荐
周排行