Max Consecutive Ones
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
分析:计算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的更多相关文章
- [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 ...
- 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 ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- 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 ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode_485. Max Consecutive Ones
485. Max Consecutive Ones Easy Given a binary array, find the maximum number of consecutive 1s in th ...
随机推荐
- loadrunner scripts
1. ReadFile: Action(){ int count,total=0; char buffer [50]; long file_stream; char * filename = &quo ...
- quartz源码分析——执行引擎和线程模型
title: quartz源码分析--执行引擎和线程模型 date: 2017-09-09 23:14:48 categories: quartz tags: [quartz, 源码分析] --- - ...
- outlook 无法搜索邮件的解决方法
我的outlook版本是2007 SP3,英文版.一直有搜索不到邮件的问题,例如在搜索框输入发件人的名字,或者邮件中的词语,就是搜索不到邮件,即使那封邮件确实存在. 在网上搜索,Microsoft 的 ...
- Java 多线程(四) 多线程访问成员变量与局部变量
先看一个程序例子: public class HelloThreadTest { public static void main(String[] args) { HelloThread r = ne ...
- JFrame常用属性设置模板
最近在学习Swing,在各种demo中都需要构建JFrame,于是我决定把构建JFrame的代码贴上来,以后就直接复制粘贴了. public static void main(String[] arg ...
- 201521123097 《JAVA程序设计》第七周学习总结
1. 本周学习总结 总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码: public boolean contains(Object ...
- 201521123034《java程序设计》第2周学习总结
1. 本章学习总结 - String对象创建之后不能再进行修改,修改字符串使用Stringbuilder: - 检测字符串内容是否相同不用==,用equals的方法检测: - 使用一维数组的两个步骤: ...
- 201521123048 《Java程序设计》第1周学习总结
一 本周学习总结 第一周我们了解了java及其它的由来.刚开始学java显的特别吃力,对于一些概念和程序执行步骤什么的都不好理解,也有很多在编程时容易出错的地方需要花时间理解和记忆.初步一周下来,在我 ...
- 201521123006 《java程序设计》 第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...
- 201521123073 《Java程序设计》第11周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问, ...