A/C Python solution, beat 22.34%, easy to understand


  • 0
    W
    def findMaxConsecutiveOnes(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
    
        lenNums = len(nums)
        if lenNums == 0:
            return 0
    
        #print "nums = ",nums
        #print "lenNums = ", lenNums
    
    
        res, zero, left, k = 0,0,0,1
    
        for right in range(lenNums):
            #print "right = ", right
            if nums[right] == 0:
                zero += 1
    
            while (zero > k):
    
                if (nums[left] == 0):
                    zero -= 1
                left+= 1
    
            res = max(res, right-left+1)
    
        #print "res = ", res
        return res

Log in to reply
 

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