Python solution


  • 0
    class Solution(object):
        def findMaxConsecutiveOnes(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            temp = res = 0
            for i in range(len(nums)):
                if nums[i]: # count number of 1s
                    temp += 1
                
                if nums[i] == 0 or i == len(nums)-1: # compare and update res when 0 or comes to the end
                    res = max(res, temp)
                    temp = 0
                
            return res
    

Log in to reply
 

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