2021-02-15 Leetcode每日刷题
题目一
给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
输入的数组只包含 0 和1。 输入数组的长度是正整数,且不超过 10,000。
我的代码:
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
res = 0
cnt = 0
for i in nums:
if i == 0:
cnt = 0
else:
cnt+=1
res = max(res,cnt)
return res
提交结果:
参考思路:
似乎每当重置为0的时候计算一下长度更为高效。