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 and 1.
  • 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;
}
}

最新文章

  1. Easy Tag Write(3.3)
  2. cmd 常用指令
  3. 1220 - Mysterious Bacteria--LightOj1220 (gcd)
  4. iOS Wi-Fi
  5. 《GK101任意波形发生器》任意波文件格式说明
  6. TCP/IP入门学习(2)---OSI分层
  7. 了解开源的许可证GPL、LGPL、BSD、Apache 2.0的区别 【转】
  8. xcode xib 加载 、注意点
  9. ElasticSearch6学习(1)-安装Elasticsearch
  10. react的this.setState没有触发render
  11. Android多线程的使用
  12. 获取mssqlserver数据库表的字段名称,字段说明,数据类型,主键等表的信息
  13. 关于idea的debug
  14. sqlalchemy 或者pysql 连接数据库时支持中文操作
  15. Shell教程 之test命令
  16. Eclipse创建Maven-Web项目及解决 jre版本和web.xml版本问题
  17. html + css + jquery实现简单的进度条实例
  18. Java之基于Eclipse搭建SSH框架(上)
  19. 【crontab】“bad minute”及“errors in crontab file, can&#39;t install”错误处理
  20. windows server 2012泛域名解析配置

热门文章

  1. 机器学习分布式框架horovod安装 (Linux环境)
  2. Ubuntu18.04 有线无法正常上网(请读完全文再进行操作)
  3. 洛谷 P2871 [USACO07DEC]手链Charm Bracelet &amp;&amp; 01背包模板
  4. 创建DateFrame的常用四种方式
  5. windows 安装svn 要点(非安装步骤)
  6. AES学习小结
  7. 清除input表单内容
  8. 1 PHP 5.3中的新特性
  9. Mybatis学习——Mybatis入门程序
  10. 吴裕雄--天生自然 JAVA开发学习:StringBuffer、数组