A Python Solution


  • 5
    L
    def findMaxConsecutiveOnes(self, nums):
        n = 0
        maxn = 0
        for i,v in enumerate(nums):
            if v == 1:
                n += 1
            else:
                maxn = max(maxn, n)
                n = 0
        maxn = max(maxn, n)
        return maxn

  • 0
    Z

    counting 1's between any two 0's

    class Solution(object):
        def findMaxConsecutiveOnes(self, nums):
            start = 0
            res = 0
    # deal with start/end case where 1's between any two 0's
            nums = [0] + nums + [0] 
            for i, num in enumerate(nums):
                if num == 0:
                    res = max(res, i - start - 1)
                    start = i
            return res
    

Log in to reply
 

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