标签:bsp href 个数 超过 维护 ons span turn ++
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; } };
[滑动窗口] leetcode 1004 Max Consecutive Ones III
标签:bsp href 个数 超过 维护 ons span turn ++
原文地址:https://www.cnblogs.com/fish1996/p/11287802.html