Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input:
[1,1,0,1,1,1]
Output:
3
Explanation:
The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
题目大意:
给定一个二进制数组,计算数组中出现的最大连续1的个数。
注意:
输入数组只包含0和1
数组长度是正整数并且不会超过10000
解题思路:
一趟遍历+计数器
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxnumber = 0
count = 0
for i in xrange(len(nums)):
if nums[i] == 1:
count += 1
maxnumber = max(maxnumber, count)
else:
count = 0
return maxnumber
Last updated
Was this helpful?