[LeetCode]485. 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.

** Note:**

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000
難度

Easy

方法

記錄每次連續(xù)1對(duì)應(yīng)1的個(gè)數(shù),保存最大值即可

python代碼
class Solution(object):
    def findMaxConsecutiveOnes(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        cur = 0
        maxCount = 0
        for i in range(len(nums)):
            if nums[i]:
                cur += 1
            else:
                if cur > maxCount:
                    maxCount = cur
                cur = 0

        return max(cur, maxCount)

Solution().findMaxConsecutiveOnes([1,1,0,1,1,1]) == 3
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,775評(píng)論 0 33
  • 女友知知跟我語音,今年特別恐懼找不到男人,腦中YY過年回家七大姑八大姨嘲諷30歲的老女人,還不把自己嫁出去,想妥協(xié)...
    DTttt閱讀 415評(píng)論 0 3