zoukankan      html  css  js  c++  java
  • [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
    

      

  • 相关阅读:
    例5-6
    例5-5
    例5-4
    例4-5
    例4-4
    例4-3
    例4-2
    例3-11
    例3-10
    例3-9
  • 原文地址:https://www.cnblogs.com/lautsie/p/12246694.html
Copyright © 2011-2022 走看看