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
 
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxn=0
cur=0 for i in nums:
if i==1:
cur+=1
else:
if cur>maxn:
maxn=cur
cur=0 if cur>maxn:
maxn=cur return maxn

  

[LeetCode&Python] Problem 485. Max Consecutive Ones的更多相关文章

  1. [LeetCode&Python] Problem 807. Max Increase to Keep City Skyline

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  2. LeetCode Array Easy 485. Max Consecutive Ones

    Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...

  3. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  4. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  5. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  6. 485. Max Consecutive Ones@python

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  7. 485. Max Consecutive Ones最长的连续1的个数

    [抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...

  8. 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  9. LeetCode 485. Max Consecutive Ones (最长连续1)

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

随机推荐

  1. 关闭provider进程或者consumer进程后,会发生什么?

    下图是 provider,consumer 和注册中心之间的拓扑图: provider,consumer 以及管理控制台都是 zookeeper 的客户端,所以都和 zk 建立了tcp连接. 以接口 ...

  2. 查看MySQL的线程

    通过两张表查看MySQL的线程:information_schema.processlist 和 performance_schema.threads processlist是information_ ...

  3. Express 路径请求及对应的获取路径形式

    req.query // GET /search?q=tobi+ferret req.query.q // => "tobi ferret" // GET /shoes?or ...

  4. OWASP TOP 10 2017中文译文

    说明:owasp top 10其实有中文官方版本:本文是按着英文版进行翻译而成. 官方中文版:http://www.owasp.org.cn/owasp-project/OWASPTop102017v ...

  5. Linux登录超时自动退出处理办法

    出于安全方面的考虑,机器常要求配置一个登录时间期限,当闲置超过这一期限就自动退出:但在某些场合我们需要时不时地就使用机器,如果每次都要重新ssh登录那是非常麻烦的 方法一:让当前会话一直处于工作状态 ...

  6. JS--label语句的使用

    使用label语句可以在代码中添加标签,以便将来使用. 一般与for循环一起使用 如: var num = 0; outermost: for(var i=0;i<10;i++){ for(va ...

  7. jquery自定义类的封装

    如何用jquery自定义一个类?(demo参考) /*简单使用*/ (function($){ //el操纵对象,option属性值 $.love = function(el,option){ var ...

  8. 逆袭之旅DAY10.东软实训.

  9. Win10系列:UWP界面布局基础9

    在XAML中,样式.模板.画笔和动画等都被看做是资源,它们最终的作用都是相同的,就是让设计人员可以使用这些资源创建更好的视觉效果,对其设计的产品外观进行标准化统一,那么如何使用这些资源并组织和重用呢, ...

  10. day7-python打开文件方式

    文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 基本操作 import codecs #主要用来解决乱码问题 f = codecs.open('1. ...