反转0,会将前一段和这一段拼起来。所以记录上一段1的个数和这一段1的个数。
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxCnt = 0
lastCnt = -1
thisCnt = 0
for i in range(len(nums)):
if nums[i] == 1:
thisCnt += 1
else: # 0
maxCnt = max(maxCnt, lastCnt + 1 + thisCnt)
lastCnt = thisCnt
thisCnt = 0
maxCnt = max(maxCnt, lastCnt + 1 + thisCnt)
return maxCnt
[leetcode]Max Consecutive Ones II
原文:https://www.cnblogs.com/lautsie/p/12246694.html