485. Max Consecutive Ones@python
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的更多相关文章
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- [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, ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- 485. Max Consecutive Ones
题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
随机推荐
- 洛谷 - P1434 - 滑雪 - 有向图最长链
https://www.luogu.org/problemnew/show/P1434 有向图的最长链怎么求?有环肯定不行,这里保证无环.(否则应该使用toposort先求出所有不带环的位置) 设dp ...
- 51NOD 1134 最长上升子序列
给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. 输入 第1行:1个数N, ...
- poj 1723 Soldiers【中位数】By cellur925
题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...
- C#语言开发规范-ching版
拙劣之处请大家斧正,愚某虚心接受,如有雷同,不胜荣幸 C#语言开发规范 作者ching 1. 命名规范 a) 类 [规则1-1]使用Pascal规则命名类名,即首字母要大写. eg: Class T ...
- AForge.net简介和认识
AForge.NET是一个专门为开发者和研究者基于C#框架设计的,他包括计算机视觉与人工智能,图像处理,神经网络,遗传算法,机器学习,模糊系统,机器人控制等领域.这个框架由一系列的类库组成.主要包括有 ...
- c++继承汇总(单继承、多继承、虚继承、菱形继承)
多重继承中,一个基类可以在派生层次中出现多次,如果一个派生类有多个直接基类,而这些直接基类又有一个共同的基类,则在最终的派生类中会保留该间接共同基类数据成员的多分同名成员.C++提供虚基类的方法使得在 ...
- Codeforces Round #408 (Div. 2) D
Description Inzane finally found Zane with a lot of money to spare, so they together decided to esta ...
- cmd命令下执行jar包程序
在cmd中使用指令来执行jar包 概述: 今天有一个需求,要在cmd中执行.jar文件 实践: 1.新建你的Hello world 2.导出到jar包 3.打开你的成功导出的jar包 4.打开文件夹 ...
- elasticsearch-sql安装
Github地址:https://github.com/NLPchina/elasticsearch-sql elasticsearch-sql插件可以方便我们使用SQL语言来对elasticsear ...
- 编程挑战JavaScript进阶篇(慕课网题目)
编程挑战 现在利用之前我们学过的JavaScript知识,实现选项卡切换的效果. 效果图: 文字素材: 房产: 275万购昌平邻铁三居 总价20万买一居 200万内购五环三居 140万安家东三环 ...