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, ...
随机推荐
- Cardboard profile的修改
Cardboard盒子中,手机屏幕大小.镜片离屏幕距离,屏幕分辨率等都会影响配戴者的眼中成像,通过对生成图像的变形可以部份解决这一问题,cardboard sdk中提供了cardboardprofil ...
- Codeforces Round #421 (Div. 2)B. Mister B and Angle in Polygon(模拟+精度控制)
传送门 题意 给出正n多边形和一个数a,寻找与a最接近的角,输出角编号 分析 找出多边形上所有角,一一比对即可 trick 1.判断的时候注意精度,i.e.x-eps>0 2.double与do ...
- 几题LCS后的小总结
先得理解最长上升子序列吧,这还是非常简单的. 然后就是要真正理解LCS: 真正理解源于做题,做题就像查漏补缺一样,你总有不会的地方. 非常彻底地理解该图(还是去做题啦) = =瞎几把乱说有两种问题 [ ...
- Comet OJ - Contest #4--前缀和
原题:Comet OJ - Contest #4-B https://www.cometoj.com/contest/39/problem/B?problem_id=1577传送门 一开始就想着暴力打 ...
- 如何使用go打出hell word
今天给大家带来一篇如何使用go打出hell word(手动滑稽) 关于go介绍的话,我就不多说了,在百度上一搜一大堆, 要使用的软件Visual Studio Code(VScode) 下载go的地址 ...
- 手把手教你如何在Fire fox火狐浏览器里在线识别下载视频(超强大)(博主推荐)
网址是 Firefox about:addons
- JSP | 基础 | 加载类失败:com.mysql.jdbc.Driver
两个原因: 1. 连接数据库需要的jar包没有导入Tomcat的lib库中 解决方案: 打开Tomcat的安装目录下的lib文件夹,把jar包拖进lib库后,重启tomcat服务器 2. mysql ...
- curry柯里化函数实现
curry柯里化函数实现 参考文章: 一行写出javascript函数式编程中的curry 感谢作者分享 第一步: 缓存原始函数的参数个数 function curry(fn) { var limit ...
- Android Platform Version 和 API Level对照
Platform Version API Level VERSION_CODE Notes Android 5.1 22 LOLLIPOP_MR1 Platform Highlights Androi ...
- myeclipse 安装svn(subeclipsesite)插件
(1)到官网下载subeclipsesite,下载最新的版本:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=224 ...