Insist on doing small things, then witness the magic
123456789101112
class Solution { public int findMaxConsecutiveOnes(int[] nums) { int left = 0, right, zeroCount = 0; for (right = 0; right < nums.length; right++) { if (nums[right] == 0) zeroCount += 1; if (zeroCount > 1 && nums[left++] == 0) zeroCount -= 1; } return right - left; }}
这个就是sliding window不缩.
时间复杂度: O(n)空间复杂度: O(1)
Calm Patient Persistent