1004. 最大连续1的个数 III

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public static int longestOnes(int[] A, int K) {
int n = A.length;
int ans = 0;
int count = 0;
int i = 0;
int j = 0;
while (j < n) {
if (A[j] == 0) {
count++;
}

while (count > K) {
// 缩小窗口
if (A[i++] == 0) {
count--;
}
}

ans = Math.max(ans, j - i + 1);
j++;
}
return ans;
}
}