[LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述
输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数
比如[1,1,0,1,1,1],输出3
思路
遍历数组,统计当前连续个数curCount和最大连续值maxCount。If当前数组值为1,则curCount++;否则(值为0)比较curCount和maxCount,看是否需要替换,并把curCount置0
最后返回maxCount
错误:对于[1]这种边界条件没有考虑完全。这种时候上面的逻辑会输出0,而应该是1。也就是结束的时候需要增加一个比较,万一如果最后的连续1的个数比较多。
——解决:在遍历完,返回之前,增加一个[比较curCount和maxCount,看是否需要替换]。
代码
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int curCount=0,maxCount=0;
for(int n: nums){
if(n==1)
curCount++;
else{
if(curCount>maxCount)
maxCount=curCount;
curCount=0;
}
}
if(curCount>maxCount)
maxCount=curCount;
return maxCount;
}
}
[LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数的更多相关文章
- LeetCode 485. Max Consecutive Ones (最长连续1)
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 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 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- 8. leetcode 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. Example 1: Input: [1, ...
- LeetCode: 485 Max Consecutive Ones(easy)
题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- ansible-playbook 编译安装nginx
mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,default,meta} -pv └── nginx ├── ...
- absorb
absorb 物理的absorb比较直观.被书本/知识absorb也好理解.涉及到money/time时有点抽象,但汉语也有"吸金"的说法,consume, use up.可以吸收 ...
- acre, across
acre The acre is a unit of land area used in the imperial and US customary systems. It is traditiona ...
- day13 iptables防火墙
day13 iptables防火墙 一.防火墙的概述 1.什么是防火墙 防止恶意流量访问的软件就叫做防火墙. 2.防火墙的种类 软件防火墙:firewalld.iptables 硬件防火墙:F5 fi ...
- JS控制元素的显示和隐藏
利用来JS控制页面控件显示和隐藏有两种方法,两种方法分别利用HTML的style中的两个属性,两种方法的不同之处在于控件隐藏后是否还在页面上占空位. 方法一: document.getElementB ...
- Java实现 HTTP/HTTPS请求绕过证书检测
java实现 HTTP/HTTPS请求绕过证书检测 一.Java实现免证书访问Https请求 创建证书管理器类 import java.security.cert.CertificateExcepti ...
- WebDriver驱动下载地址
chrome的webdriver: http://chromedriver.storage.googleapis.com/index.html Firefox驱动下载地址为:https://githu ...
- InnoDB学习(四)之RedoLog和UndoLog
BinLog是MySQL Server层的日志,所有的MySQL存储引擎都支持BinLog.BinLog可以支持主从复制和数据恢复,但是对事务的ACID特性支持比较差.InnoDB存储引擎引入Redo ...
- 转:builder模式分析
建造者模式 11.1 变化是永恒的 又是一个周三,快要下班了,老大突然拉住我,喜滋滋地告诉我:"牛叉公司很满意我们做的模型,又签订了一个合同,把奔驰.宝马的车辆模型都交给我们公司制 作了,不 ...
- aha
欢迎使用 MWeb MWeb 是专业的 Markdown 写作.记笔记.静态博客生成软件,目前已支持 Mac,iPad 和 iPhone.MWeb 有以下特色: 软件本身: 使用原生的 macOS 技 ...