每日一题力扣485

题目:给定一个二进制数组, 计算其中最大连续 1 的个数。

错解:

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

错误在于循环体外漏了一个判断max的

 

正解:

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

 

posted @ 2021-02-22 19:36  小千北同学超爱写代码  阅读(67)  评论(0编辑  收藏  举报