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

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
猜你喜欢
  • 2022-12-23
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
相关资源
相似解决方案