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