python two pointers


  • 0
    X
    class Solution(object):
        def findMaxConsecutiveOnes(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            count = None
            sl,fa = 0,0
            m = 0
            while fa<len(nums):
                if nums[fa]==1:
                    fa += 1
                    m = max(m,fa-sl)
                elif None != count:
                    sl = count + 1
                    while nums[sl]==0:
                        sl += 1
                        if sl == len(nums):
                            break
                    if sl>fa:
                        fa = sl
                        count = None
                    else:
                        count = fa
                        fa += 1
                else:
                    count = fa
                    fa += 1
                    m = max(m,fa-sl)
            return m
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.