原题目连接:
485. 最大连续 1 的个数 - 力扣(LeetCode)
这个题里,让我们计算其中最大连续1的个数,之间遍历数组即可,我们先设置两个变量,一个是count,用来统计连续1的个数,遇到0就会清零,然后是max变量,当遇到0的时候就和count进行比较,统计1的最大值
代码如下:
int findMaxConsecutiveOnes(int* nums, int numsSize){
int count = 0;
int max = 0;
for(int i = 0;i < numsSize;i++)
{
if(nums[i] == 1)
{
count++;
}
else
{
if(max < count)
{
max = count;
}
count = 0;
}
}
if(max < count)
{
max = count;
}
return max;
}
其中,最后还要max和count进行比较一下,是因为如果出现【1,1,0,1,1,1】这种最后没有0的情况