485. Max Consecutive Ones 最长连续1序

2017-07-30  本文已影响0人  这就是一个随意的名字

Given a binary array, find the maximum number of consecutive 1s in this array.
给定一二进制序列,找出其中最大的连续为1的序列的元素数目。

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.

Note:

注:


思路:

class Solution(object):
    def findMaxConsecutiveOnes(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res=0
        cur=0
        for i in nums:
            if i==1:
                cur+=1
            else:
                cur=0
            if cur>res:
                res=cur
        return res
上一篇 下一篇

猜你喜欢

热点阅读