首页 > 其他 > 详细

每日一题力扣485

时间:2021-02-22 20:20:19      阅读:22      评论:0      收藏:0      [点我收藏+]

题目:给定一个二进制数组, 计算其中最大连续 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

 

每日一题力扣485

原文:https://www.cnblogs.com/liuxiangyan/p/14432350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!