网址:https://leetcode.com/problems/max-consecutive-ones-iii/
参考:https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/JavaC%2B%2BPython-Sliding-Window
- sliding window
- K -= (A[j] == 0);
- 注意判断条件
1 class Solution { 2 public: 3 int longestOnes(vector<int>& A, int K) { 4 int i = 0; 5 int ans = 0; 6 for(int j = 0; j<A.size(); j++) 7 { 8 K -= (A[j] == 0); 9 if(K < 0) 10 { 11 K += (A[i] == 0); 12 i++; 13 } 14 ans = j - i + 1; 15 } 16 return ans; 17 } 18 };