题目
485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
解题思路
遍历整个数组,用t计算连续的1的个数。如果遇到1,t加1;如果遇到0,比较t与曾经出现过的最长的1的个数,如果t比较大,就更新maxOne,然后t归0。最后返回maxOne即可
代码
class Solution:
def findMaxConsecutiveOnes(self, nums):
t = 0
i = 0
maxlen = 0;
while i < len(nums):
if nums[i]:
t += 1;
else :
if maxlen < t:
maxlen = t;
t = 0;
i += 1
if maxlen < t:
maxlen = t;
return maxlen
if __name__ == '__main__':
s = Solution()
print(s.findMaxConsecutiveOnes([1,1,1,0,1,1]));