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.

原题地址: Max Consecutive Ones

难度: Easy

题意: 找出连续为1的子数组的最大长度

思路1:

因为只有0, 1,可以通过确定0的位置确定1的长度.但需要考虑边界问题,可以假设 -1位置 和 len(nums) 位置都为0,边界问题就可以解决了

索引:   0    1    2    3    4    5    6
数值: 1 1 1 1 0 1 1

变为:

索引:   -1    0    1    2    3    4    5    6
数值: 0 1 1 1 1 0 1 1 0
第一组连续1的子数组长度: 4 - (-1) - 1 = 4
第二组连续1的子数组长度: 7 - (4) - 1 = 2

代码:

class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
j = -1 # 初始化0的位置
res = 0
for i in range(len(nums)):
if nums[i] == 0:
res = max(res, i-j-1)
j = i
res = max(res, len(nums)-j-1)
return res

思路2:

遍历数组,统计1的个数

代码:

class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
count = 0
for i in range(len(nums)):
if nums[i] == 1:
count += 1
res = res if res > count else count
else:
count = 0
return res

时间复杂度: O(n)

空间复杂度: O(1)

485. Max Consecutive Ones@python的更多相关文章

  1. 【leetcode】485. Max Consecutive Ones

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

  2. 485. Max Consecutive Ones【easy】

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

  3. 485. Max Consecutive Ones - LeetCode

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

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

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

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

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

  6. [LeetCode&Python] Problem 485. Max Consecutive Ones

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

  7. LeetCode 485 Max Consecutive Ones 解题报告

    题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...

  8. 485. Max Consecutive Ones

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

  9. 8. leetcode 485. Max Consecutive Ones

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

随机推荐

  1. E20180404-ts

    aero  adj. 航空的,飞行的,飞机的; wheel   n. 轮子; 旋转; Wheels 汽车; 有…轮子的汽车(或自行车等); vi. 转动,旋转; 突然转变方向; foil n. 箔,金 ...

  2. bzoj 1022: [SHOI2008]小约翰的游戏John【anti-nim】

    如果全是1,那么n是奇数先手必败 否则,xor和为0先手必败 证明见 https://www.cnblogs.com/Wolfycz/p/8430991.html #include<iostre ...

  3. IT兄弟连 Java Web教程 Tomcat

    本文采用的Tomcat服务器版本是Tomcat8.5版本,Tomcat8.5支持Servlet3.1.JSP2.3以及EL3.0规范.并且Tomcat8.5版本对JDK8的支持比Tomcat8更加全面 ...

  4. eclipse中alt+/的作用

    一般情况下alt+/有代码提示作用,还有代码提示的快捷代码也不是alt+/,因此要恢复代码提示用alt+/.需要做两件事.在 Window - Preferences - General - Keys ...

  5. 关于AFNetWorking 2.5.4之后版本编译报错问题解决方案

    最近升级了AFN框架到2.6版本然后编译却出错了 错误如下: 错误出现在 AFSecurityPolicy.h 这个类中 解决办法如下: 在项目的.pch文件里添加 #ifndef TARGET_OS ...

  6. bzoj 4513 [Sdoi2016]储能表

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4513 题解 要求的式子 用数位dp的方法去做 我们把式子拆开 变成 $\sum_{i=0}^ ...

  7. SpringMVC配置文件-web.xml的配置

    SpringMVC配置文件(重点) @Web.xml @核心拦截器(必配) <!-- spring 核心转发器,拦截指定目录下的请求,分配到配置的拦截路径下处理 --> <servl ...

  8. 151 Reverse Words in a String 翻转字符串里的单词

    给定一个字符串,翻转字符串中的每个单词.例如,给定 s = "the sky is blue",返回 "blue is sky the".对于C程序员:请尝试用 ...

  9. 145 Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

  10. ibatis学习笔记

    步骤: 搭建配置环境:导入相关jar包 配置文件: JDBC连接属性文件 总配置文件 关于每个实体的映射(map.xml)文件 JDBC连接属性文件 jdbc.properties ## mysql ...