一、题目解析
二、算法思路
三、代码实现
class Solution {
public:
int longestOnes(vector<int>& nums, int k)
{
int ret=0,count=0;
int left=0,right=0;
for (; right < nums.size();right++ )
{
if (nums[right] == 0)
{
count++;
}
if (count > k)
{
//更新ret
ret = max(ret, right - left );
count -= 1;
//left跳到[left,right]区间中第一个0后面
int regist = 0;
while (regist<1)
{
if (nums[left] == 0)
{
regist++;
}
left++;
}
}
}
if(count<=k)
{
ret=max(ret,right-left);
}
return ret;
}
};