485. Max Consecutive Ones 最长连续1序列

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:

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

注:

  • 输入序列只包含0和1
  • 输入序列的长度为正整数且不会超过10000

思路:

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 本文参加#青春不一YOUNG#征稿活动,本人承诺,文章内容为原创,且未在其他平台发表过。 你好,我是杞萌。我...
    杞萌阅读 537评论 0 0
  • 理财这件事情吧,总是听各种各样的人在说,有的时候自己也会说几句,说得多了就觉得自己是个理财高手。但是说实话,说得再...
    gh9527阅读 231评论 0 0
  • 1. 说一说你平时写代码遵守的编码规范结合我自身有限的编码经验,谈谈我目前遵守的编码规范:(1)尽量选取语义化的标...
    饥人谷_半岛王子阅读 640评论 0 49
  • 学了学二叉树,这里说说怎样遍历二叉树.四种方式:前序遍历,中序遍历,后序遍历,层次遍历. 主要说说递归的遍历方法前...
    Anxdada阅读 737评论 0 0