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, ...
随机推荐
- (DP)51NOD 1007正整数分组
将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 输入 第1行:一个数N,N为正整数的数量. 第 ...
- PAT 1035 插入与归并(25)
原题:https://pintia.cn/problem-sets/994805260223102976/problems/994805286714327040传送门: 根据维基百科的定义: 插入排序 ...
- python之yaml模块和ddt模块
aml文件是专门用来写配置文件的语言,非常简洁和强大,远比json格式方便. 在PC中新建一个yml/yaml为为缩略名的文件,输入信息见下图 新建一个py文件处理yml文件,直接处理成字典格式 缩进 ...
- bzoj 5019 [Snoi2017]遗失的答案
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=5019 题解 如果L不是G的倍数 答案为0 下面考虑G|L的情况 将G,L质因数分解 设$L= ...
- EOJ Monthly
###2018.10 A.oxx 的小姐姐们 oxx 和他的小姐姐(们)躺在图书馆前的大草坪上看星星. 有强迫症的 oxx 想要使得他的小姐姐们正好躺成一块 n×m 的长方形. 已知小姐姐的形状是 1 ...
- bootstrap div 固定
div固定在顶部样式: .navbar-fixed-top div固定在底部样式 .navbar-fixed-bottom
- asp.net core教程 (一)
Asp.Net Core简介 ASP.NET Core 是一个全新的开源.跨平台框架,可以用它来构建基于网络连接的现代云应用程序,比如:Web 应用,IoT(Internet Of Things,物联 ...
- 通俗易懂的Nhibernate教程(2) ---- 配置之Nhibernate配置
在上一个教程中,我们讲了Nhibernate的基本使用!So,让我们回顾下Nhibernate使用基本的步骤吧 1.NHibernate配置 ----- 这一步我们告诉了Nhibernate:数据库 ...
- flask搭建
1.定义路由app.py from flask import Flask, request from flask import Blueprint app = Flask(__name__) test ...
- 【学习笔记】HTML position(static、fixed、relative、absolute)
[本文转载] position的四个属性值:static.fixed.relative.absolute 下面分别讲述这四个属性:<div id="parent"> ...