其他分享
首页 > 其他分享> > [leetcode]Max Consecutive Ones II

[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

  

标签:thisCnt,lastCnt,nums,int,Max,II,Ones,一段,maxCnt
来源: https://www.cnblogs.com/lautsie/p/12246694.html