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

分析:计算1连续出现的最大次数。

思路:遍历统计次数,记录下连续出现的次数即可。

JAVA CODE

class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int times = 0, maxTimes = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 0){
maxTimes = maxTimes > times ? maxTimes : times;
times = 0;
}else{
times++;
}
}
return maxTimes = maxTimes > times ? maxTimes : times;
}
}
 

Max Consecutive Ones的更多相关文章

  1. [LeetCode] Max Consecutive Ones II 最大连续1的个数之二

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  2. Leetcode: Max Consecutive Ones II(unsolved locked problem)

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  3. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  4. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  5. 485. Max Consecutive Ones最长的连续1的个数

    [抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...

  6. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  7. LeetCode Max Consecutive Ones II

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...

  8. Flume启动报错[ERROR - org.apache.flume.sink.hdfs. Hit max consecutive under-replication rotations (30); will not continue rolling files under this path due to under-replication解决办法(图文详解)

    前期博客 Flume自定义拦截器(Interceptors)或自带拦截器时的一些经验技巧总结(图文详解)   问题详情 -- ::, (SinkRunner-PollingRunner-Default ...

  9. 485. Max Consecutive Ones@python

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  10. LeetCode_485. Max Consecutive Ones

    485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in th ...

随机推荐

  1. ASP.NET静态化方法

    直接通过访问页面获取html代码实现静态化 突然想到一个静态化页面的方法:直接保存源代码即可. 模拟浏览器访问,获得源码,写入文件.不知道是否存在安全风险:各位大神请指点: 注意 1.资源使用绝对路径 ...

  2. Spring-MVC请求参数值和向页面传值

    读取请求参数值 方式一:通过HttpServletRequest req做参数 DispatcherServlet在调用处理的方法之前,利用Java反射分析方法的结构,通过分析,将req对象传过来 方 ...

  3. javascript学习笔记-4

    document.getElementByTagName返回的是一个NodeList,这个NodeList和js数组很类似,都可以使用下标读取,如:array[0],但他们也有不同,不同在于不能对No ...

  4. 通过业务系统的重构实践DDD

    最近新接了一个业务系统——社区服务系统,为了快速熟悉和梳理老系统的业务逻辑和代码,同时对老系统代码做一些优化,于是打算花上一个月时间不间断地对老系统服务进行重构.同时,考虑到社区业务的复杂性,想起了之 ...

  5. android-学习1 配置环境

    转载注明出处,谢谢--http://www.cnblogs.com/JK-HYJ/ 前提:电脑先配置好java的环境-- adk_bundle下载安装地址:http://tools.android-s ...

  6. 02-TypeScript中新的字符串

    TypeScript中引入了字符串模板,通过字符串模板可以方便的实现字符串换行的连接.方便变量等. 1.在WebStorm中新建一个文件,后缀名为ts. 在建立ts文件时,WebStorm会问你是否需 ...

  7. selenium chromedriver与谷歌浏览器版本映射表 (更新至v2.32)

    ----------ChromeDriver v2.32 (2017-08-30)---------- Supports Chrome v59-61最新的ChromeDriver 2.32 支持谷歌浏 ...

  8. 团队作业8----第二次项目冲刺(beta阶段)5.25

    Day7-05.25 1.每日会议 会议内容: 1.今日对整个项目进行了一个总结. 2.讨论了这次项目中的不足和每个人的贡献. 讨论照片:拍摄者 周迪 2.任务分配情况: 每个人的工作分配表: 队员 ...

  9. 线程高级篇-Lock锁和Condition条件

    浅谈Synchronized: synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其 ...

  10. 201521123049 《JAVA程序设计》 第7周学习总结

    1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 //contains()方法 public boolean contains ...