leetcode-485. 最大连续1的个数
一、问题描述
给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
- 输入的数组只包含
0
和1
。 - 输入数组的长度是正整数,且不超过 10,000。
二、代码和思路
因为连续1的和即为连续1的个数,所在我们只要求最大连续1的值即求最大连续1的个数
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i,max_count,count,n=0,0,0,len(nums)
while i<n:
while i<n and nums[i]==1: #这里循环碰到0就停止并统计1的和
count += 1
i += 1
if count>max_count: #max_count记录最大的count
max_count=count
count=0 #重新从另一个连续1开始计数
i += 1
return max_count
三、运行结果(算法效果还行)