LeetCode 485:连续最大1的个数 Max Consecutive Ones(python java)
公众号:爱写bug
给定一个二进制数组, 计算其中最大连续1的个数。
Given a binary array, find the maximum number of consecutive 1s in this array.
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
- 输入的数组只包含
0和1。 - 输入数组的长度是正整数,且不超过 10,000。
Note:
- The input array will only contain
0and1. - The length of input array is a positive integer and will not exceed 10,000
解题思路:
记录一个指针向右移动,用一个数记录1的个数,遇1就累加1,遇0就倒置为0。具体见 Java 注释。
Java:
class Solution{
public int findMaxConsecutiveOnes(int[] nums) {
int temp=0,count=0;//temp记录当前连续1的个数,count记录当前最大连续1的个数
for (int i=0;i<nums.length;i++){//指针右移
if(nums[i]==1){
temp++;//遇1累加1
}else{
if(count<temp){
count=temp;//记录目前最大连续1的个数
}
temp=0;//遇0倒置为0
}
}
return (count>temp)? count:temp;//返回count、temp中较大的数
}
}
注意:
返回值必须是count 与temp 中较大的一个。明明已经比较了count和temp,并把较大的赋值给count ,很明显是count 更大,为什么还要比较?
这是因为还有一种输入数组全为1的情况,此时temp一直累加,从未遇到0,所以count自始至终都不可能得到temp的值。
python3:
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count=temp=0
for num in nums:
if num==1:
temp+=1
else:
if(count<temp):
count=temp
temp=0
return count if count>temp else temp
LeetCode 485:连续最大1的个数 Max Consecutive Ones(python java)的更多相关文章
- 485. 找出二进制串中连续的1的个数 Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- [Swift]LeetCode485. 最大连续1的个数 | Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode算法题-Max Consecutive Ones(Java实现)
这是悦乐书的第242次更新,第255篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第109题(顺位题号是485).给定二进制数组,找到此数组中连续1的最大数量.例如: 输 ...
- leetcode解题报告(14):Max Consecutive Ones
描述 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
- [LeetCode] Max Consecutive Ones II 最大连续1的个数之二
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- Leetcode 485. 最大连续1的个数
1.题目描述(简单题) 给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 ...
随机推荐
- Docker入门学习及其安装
1.Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源.Docker可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的Linux机器 ...
- C# winform打开新窗体显示一段时间 关闭新窗体
1.form1的button事件下: form2 form = new form2(); form.Show(); Thread.Sleep(10000); //form2窗体显示10秒 form. ...
- 关于@HtmlHelper帮助器参数
@Html.ActionLink("首页", "Index", "Index", new{},new { @class = "na ...
- Python传入参数的几种方法
写在前面 Python唯一支持的参数传递方式是『共享传参』(call by sharing) 多数面向对象语言都采用这一模式,包括Ruby.Smalltalk和Java(Java的引用类型是这样,基本 ...
- pip下载速度慢解决方法
添加镜像链接 解决方式: 更改pip的数据源.目前国内比较知名的有豆瓣的,清华的.都是pipy官网的镜像. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里 ...
- ** PC端完美兼容各种分辨率的简便方法 **
原文链接:https://blog.csdn.net/qq_43156398/article/details/102785370 PS:此方法需使用到less或者scss的@变量来支持 以设计图 19 ...
- 绕过基于签名的XSS筛选器:修改HTML
绕过基于签名的XSS筛选器:修改HTML 在很多情况下,您可能会发现基于签名的过滤器只需切换到一个不太熟悉的执行脚本的方法即可.如果失败了,您需要查看混淆攻击的方法. 本文提供了HTML语法可以被混淆 ...
- Android实用的Toast工具类封装
Toast这个提示框大家都晓得,显示一段时间后自动消失,不能获得焦点.但是在使用中有些问题: 1)需要弹出一个新的Toast时,上一个Toast还没有显示完2)可能重复弹出相同的信息3)Toast具体 ...
- Android不显示开机向导和开机气泡
修改好的代码下载地址: https://github.com/Vico-H/Launcher 不显示开机向导 修改Launcher2.java的代码 (文件位置: /alps/packages/app ...
- Python Scrapy在windows上的安装方法
如果想要学习爬虫,肯定会了解Scrapy,但安装Scrapy的方法相对于安装其他第三方库麻烦一点. 下面总结一下在我的电脑上安装Scrapy的方法,我的电脑是Windows10,32位操作系统.有如下 ...