[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 ...
随机推荐
- C++类虚函数内存分布(这个 你必须懂)
转自:http://www.cnblogs.com/jerry19880126/p/3616999.html C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来 ...
- C++/Python冒泡排序与选择排序算法详解
冒泡排序 冒泡排序算法又称交换排序算法,是从观察水中气泡变化构思而成,原理是从第一个元素开始比较相邻元素的大小,若大小顺序有误,则对调后再进行下一个元素的比较,就仿佛气泡逐渐从水底逐渐冒升到水面一样. ...
- 使用 JDBC 驱动程序
本部分提供使用 Microsoft JDBC Driver for SQL Server 与 SQL Server 数据库建立简单连接的快速入门指导.在连接到 SQL Server 数据库之前,必须首 ...
- A Child's History of England.5
Above all, it was in the Roman time, and by means of Roman ships, that the Christian Religion was fi ...
- A Child's History of England.39
He had become Chancellor, when the King thought of making him Archbishop. He was clever, gay, well e ...
- Jenkins:参数化构建:分支|模块|回滚|打印日志
@ 目录 多分支 安装Git Parameter Plug-In 配置参数 选择构建分支 分模块 前提 分模块build 参数配置 分模块shell脚本 mvn 的基本用法 分模块运行 Jenkins ...
- 【Other】逻辑分析仪的使用(UART、SPI)
首先上一张接线示意图 上方是UART的接线方式,下方则是SPI的 事实上,这样接就能收到信号了 如果是SPI,要设定自己为主机,UART则没有这个问题 下面来说明逻辑分析仪的界面设定 设定介绍完了 下 ...
- How is Quality Score Calculated?
Google determines Quality Score slightly differently for each of the different advertising networks ...
- 如何从 100 亿 URL 中找出相同的 URL?
题目描述 给定 a.b 两个文件,各存放 50 亿个 URL,每个 URL 各占 64B,内存限制是 4G.请找出 a.b 两个文件共同的 URL. 解答思路 每个 URL 占 64B,那么 50 亿 ...
- html href页面跳转获取参数
//传递参数 var id = columnData.id; var companyname = encodeURI(columnData.companyname); var linename = e ...