[LC] 485. Max Consecutive Ones
2024-09-07 05:17:04
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is 3.
Note:
- The input array will only contain
0
and1
. - The length of input array is a positive integer and will not exceed 10,000
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = 0, cur = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (i == 0 || nums[i - 1] == 1) {
cur += 1;
} else {
cur = 1;
}
// case [0, 1] res cover both cases
res = Math.max(res, cur);
}
}
return res;
}
}
最新文章
- Easy Tag Write(3.3)
- cmd 常用指令
- 1220 - Mysterious Bacteria--LightOj1220 (gcd)
- iOS Wi-Fi
- 《GK101任意波形发生器》任意波文件格式说明
- TCP/IP入门学习(2)---OSI分层
- 了解开源的许可证GPL、LGPL、BSD、Apache 2.0的区别 【转】
- xcode xib 加载 、注意点
- ElasticSearch6学习(1)-安装Elasticsearch
- react的this.setState没有触发render
- Android多线程的使用
- 获取mssqlserver数据库表的字段名称,字段说明,数据类型,主键等表的信息
- 关于idea的debug
- sqlalchemy 或者pysql 连接数据库时支持中文操作
- Shell教程 之test命令
- Eclipse创建Maven-Web项目及解决 jre版本和web.xml版本问题
- html + css + jquery实现简单的进度条实例
- Java之基于Eclipse搭建SSH框架(上)
- 【crontab】“bad minute”及“errors in crontab file, can&#39;t install”错误处理
- windows server 2012泛域名解析配置