problem:https://leetcode.com/problems/max-consecutive-ones-iii/
维护最多包含k个0的滑动窗口,一旦超过了k个0,把队首的0 pop出来。不断更新当前滑动窗口中的数据个数,并取最大值返回即可。
class Solution { public: int longestOnes(vector<int>& A, int K) { int count = 0; int index = -1; deque<int> zeros; int res = 0; for (int i = 0; i < A.size(); i++) { if (A[i] == 0) { zeros.push_back(i); } if (zeros.size() > K) { index = zeros.front(); zeros.pop_front(); } res = max(res, i - index); } return res; } };
原文地址:https://www.cnblogs.com/fish1996/p/11287802.html
时间: 2024-10-24 21:49:52