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的个数是 ...
随机推荐
- ASP.NET Core快速入门(第3章:依赖注入)--学习笔记
课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务16:介绍 1.依赖注入概念详解 从UML和软件建模来理解 从单元测试来理 ...
- Flask--数据库连接池
目录 数据库连接池 pymsql链接数据库 数据库连接池版 数据库连接池 pymsql链接数据库 import pymysql conn = pymysql.connect(host='127.0.0 ...
- C# 常用类库(字符串处理,汉字首字母拼音,注入攻击,缓存操作,Cookies操作,AES加密等)
十年河东,十年河西,莫欺少年穷 学无止境,精益求精 记录下字符串类库,方便今后查阅 主要包含了字符串解决,去除HTML,SQL注入攻击检测,IP地址处理,Cookies操作,根据身份证获取性别.姓名. ...
- Scrum冲刺博客
一.各个成员在Alpha阶段认领的任务 已完成 二.各个成员的任务安排 三.整个项目期的任务量 按实际考试情况以及开发情况决定,初始计划是完成登录以及个人目标版块的完整功能,其它版块共进,保证最终能够 ...
- [转]技术比较Agent和Agentless监控优缺点
本文并非原创,转自:http://wenku.baidu.com/link?url=NGT2NA7Lf6fZSPmcOxFQqL4cYROHlIOJyiWCnGdwv3kljMqub-6zyjgsSw ...
- 【原创】Talend 配置SSL支持gitlab
背景 talend的源代码控制用的是gitlab,以前都是http方式的,但是最近突然改了https,所以talend登录失败,必须要SSL方式才能获取到分支等数据,才能提交代码. 证书导入 1.ta ...
- 封装tab组件
=====>tab切换组件的封装 wx:key="{{index}} 绑定标识 它的下标是从0开始的 {{currentIndex==index ? "active" ...
- Flask框架整理及配置文件
阅读目录 Flask目录结构(蓝图) pro_flask包的init.py文件, 用于注册所有的蓝图 manage.py文件,作为整个项目的启动文件 views包中的blog.py,必须要通过sess ...
- opencv旋转图像
#include <opencv2\opencv.hpp> /* @param o The customer origin @param x The customer x @Note Th ...
- mysql版本引起的驱动问题
mysql版本引起的驱动问题 com.mysql.jdbc.Driver 是 mysql-connector-java 5中的, com.mysql.cj.jdbc.Driver 是 mysql-co ...