题目要求

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

题目分析及思路

给定一个01数组,要求找到这个数组中连续1的最大长度。可以考虑0的位置,结合数组切片,循环进行。要记得把最后数组的长度加上。

python代码

class Solution:

def findMaxConsecutiveOnes(self, nums: List[int]) -> int:

if len(set(nums)) == 1 and nums[0] == 1:

return len(nums)

ones = []

while 0 in nums:

zero = nums.index(0)

ones.append(zero)

nums = nums[zero+1:]

ones.append(len(nums))

return max(ones)

LeetCode 485 Max Consecutive Ones 解题报告的更多相关文章

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

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

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

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

  3. 8. leetcode 485. Max Consecutive Ones

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

  4. (双指针) leetcode 485. Max Consecutive Ones

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

  5. LeetCode: 485 Max Consecutive Ones(easy)

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

  6. [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数

    题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...

  7. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  8. 【leetcode】485. Max Consecutive Ones

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

  9. 485. Max Consecutive Ones - LeetCode

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

随机推荐

  1. vue2.0 实现click点击当前li,动态切换class

    1,文件内容 ----//为item添加不存在的属性,需要使用vue提供的Vue.set( object, key, value )方法.  看详解:https://cn.vuejs.org/v2/a ...

  2. Thinkphp的S缓存用法!

    /节点列表 public function node(){ if(!$node = S('node_list')){ $field = array('id', 'name', 'title', 'pi ...

  3. Serializable接口

    Serializable这个接口起啥作用呢?? 这个接口没有提供任何方法,我们实现它有什么意义呢? Serializable接口是启用其序列化功能的接口.Serializable接口中没有任何方法,一 ...

  4. 【css】css 中文字体 unicode 对照表

    css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...

  5. sparkR介绍及安装

    sparkR介绍及安装 SparkR是AMPLab发布的一个R开发包,为Apache Spark提供了轻量的前端.SparkR提供了Spark中弹性分布式数据集(RDD)的API,用户可以在集群上通过 ...

  6. MySQL数据库远程访问权限如何打开(两种方法)

    在我们使用mysql数据库时,有时我们的程序与数据库不在同一机器上,这时我们需要远程访问数据库.缺省状态下,mysql的用户没有远程访问的权限. 下面介绍两种方法,解决这一问题. 1.改表法 可能是你 ...

  7. react-router 4.3 js实现跳转

    import React, {Component} from 'react'; import { NavLink,Link } from "react-router-dom"; i ...

  8. Maven支撑下的War应用依赖另外一个WAR应用的解决方案

    最近在做项目中,用Maven管理项目间的依赖关系,遇到一个问题,快折腾死了,不过初步试出来一种解决方案.在此把问题及解决方案描述一下,以资共享.   问题描述:有两个项目A和B,Dynamic Web ...

  9. tomcat 下安装 MantisBT

    环境 OS:win8.1 up1 64bit tomcat :9.0.0 64bit php: php-7.1.7-nts-Win32-VC14-x64.zip postgres: postgresq ...

  10. IntellIJ IDEA 启动 参数 配置

    系统环境: 型号名称: MacBook Pro型号标识符: MacBookPro11,4处理器名称: Intel Core i7处理器速度: 2.8 GHz处理器数目: 1核总数: 4L2 缓存(每个 ...