Max Consecutive Ones II

这题可以用传统双指针,但是那样会首先需要对第一个0的位置单独处理一个逻辑,代码变得很复杂,这里可以用三个位置构成两个全1区域,然后通过由前往后传值来确定最大的距离,这样代码简洁很多

class Solution(object):
    def findMaxConsecutiveOnes(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        start, middle, end = -1, -1, -1
        result = 0
        for index, num in enumerate(nums):
            if not num:
                result = max(result, end - start - 1) 
                start = middle
                middle = end
                end = index

        return max(result, len(nums) - middle - 1)

results matching ""

    No results matching ""