LeetCode 485 Max Consecutive Ones (Python)

Posted by 小明MaxMing on February 21, 2022

题目

Given a binary array nums, return the maximum number of consecutive 1’s in the array.

解题思路

遍历数组,遇到1计数器+1,遇到0更新结果同时计数器清零,单独处理最后一位不是0的情况

代码

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        res = tmp = 0
        for n in nums:
            if n == 1:
                tmp += 1
            else:
                res = max(res, tmp)
                tmp = 0
        return max(res, tmp)

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅