给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
- 输入的数组只包含
0和1。 - 输入数组的长度是正整数,且不超过 10,000。
class Solution:
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
list3 = []
for i in nums:
if i != 1:
index = nums.index(i)
list2 = nums[:index:]
count = len(list2)
list3.append(count)
nums = nums[index + 1::]
if not 0 in nums:
list3.append(len(nums))
return max(list3)
s1 = Solution()
print(s1.findMaxConsecutiveOnes([1, 1, 0, 1, 1, 1]))
本文介绍了一种计算给定二进制数组中最大连续1的个数的方法。通过遍历数组并记录连续1的序列长度,最终找出最长的连续1子序列。文章提供了一个具体的示例,输入为[1,1,0,1,1,1]时,输出结果为3。

1741

被折叠的 条评论
为什么被折叠?



