leetcode刷题题解(含main函数可运行)
滑动窗口
485.最大连续1的个数
//最大连续1的个数
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int findMaxConsecutiveOnes(int* nums, int numsSize){
int i;
int count=0;
int max = 0;
for ( i = 0; i < numsSize; i++)
{
if (nums[i]==1)
{
count++;
max = fmax(count,max);
}else{
count = 0;
}
}
return max;
}
int main(){
int size = 10;
int* s = (int *)calloc(size,sizeof(int));
int i = 0;
printf("nums= ");
do
{
scanf("%d",&s[i]);
i++;
} while (getchar()!='\n');
size = i;
int result = findMaxConsecutiveOnes(s,size);
printf("%d\n",result);
}
新手,解题肯定是不完美的,基础薄弱的朋友可以参考,先有思路,慢慢做的多了可以再优化,有更好思路的朋友可以留言,大家一起进步!