[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 ...
随机推荐
- 3.Median of Two Sorted Arrays Leetcode
难度系数:5星 /*************************************************************************** 题目:有两个排好序的数组,要求 ...
- Yarn 容量调度器多队列提交案例
目录 Yarn 容量调度器多队列提交案例 需求 配置多队列的容量调度器 1 修改如下配置 SecureCRT的上传和下载 2 上传到集群并分发 3 重启Yarn或yarn rmadmin -refre ...
- Docker学习(四)——Docker容器连接
Docker容器连接 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过-P或-p参数来指定端口映射. 下面我们来实现通过端口连接到一个docker容器. 1.网络端口映射 ...
- HelloWorldDynamic
package mbeanTest; import java.lang.reflect.Method; import javax.management.Attribute; import javax. ...
- html href页面跳转获取参数
//传递参数 var id = columnData.id; var companyname = encodeURI(columnData.companyname); var linename = e ...
- redis的总结笔记
# Redis 1. 概念: redis是一款高性能的NOSQL系列的非关系型数据库 1.1.什么是NOSQL NoSQL(NoSQL = Not Only ...
- 使用$.post方式来实现页面的局部刷新功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 注册页面css版本
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 网络访问控制列表ACL(读懂这篇就基本够了,后面有配置案例)
一.访问控制列表是什么? 访问控制列表(ACL)是一种基于包过滤的访问控制技术,它可以根据设定的条件对接口上的数据包进行过滤,允许其通过或丢弃.访问控制列表被广泛地应用于路由器和三层交换机,借助于访问 ...
- 项目开发中,真的有必要定义VO,BO,PO,DO,DTO这些吗?
存在即是合理的,业务复杂,人员协同性要求高的场景下,这些规范性的东西不按着来虽然不会出错,程序照样跑,但是遵守规范会让程序更具扩展性和可读性,都是前辈血淋淋的宝贵经验,为什么不用? 随着现在后端编程标 ...