Python straight forward solution with a little better naming


  • 0
    P

    Just keep track of with or without the additional 1.

    class Solution(object):
        def findMaxConsecutiveOnes(self, nums):
            if not nums:
                return 0
            cnt_with_one = 0 #count of ones with the additional 1
            cnt_without_one = 0 # without the additional 1
            res = 0
            for i in range(len(nums)):
                if nums[i] == 0:
                    cnt_with_one = cnt_without_one+1
                    cnt_without_one = 0
                else:
                    cnt_with_one+=1
                    cnt_without_one+=1
                res = max(res, cnt_with_one)
            return res
    

Log in to reply
 

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