又用到滑动窗口,关键在于要维持窗口内0的数量在k内
#include<stdio.h>
#include<string.h>int Max(int a,int b){return (a>b)?a:b;
}int findMaxConsecutiveOnes(int* nums,int numsSize,int k){int left = 0;int right = 0;int flag = 0;//记录包含的0的个数 int i,j;int max = 0;while(right<numsSize){if(nums[right]==0)flag++;//如果窗口内0超过k,则移动左指针维持0的个数 while(flag>k){if(nums[left]==0){flag--;}left++;}max = Max(max,right-left+1);right++;}return max;
}
int main()
{int nums[] = {1,1,1,0,0,0,1,1,1,1,0};int numsSize = sizeof(nums)/sizeof(nums[0]);int k = 2; int max = findMaxConsecutiveOnes(nums,numsSize,k);printf("%d",max);return 0;
}