[leetcode]Max Consecutive Ones II

反转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

  

posted @ 2020-01-31 20:56  阿牧遥  阅读(96)  评论(0编辑  收藏  举报