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

题目标签:Array

  题目给了我们一个nums array, 里面只会出现0和1, 让我们找到一个最长连续1的长度。

  这题很简单,只要维护一个maxCount 就可以了,再设一个count,遇到1的时候,count++;遇到0的时候,把count 和 maxCount 中挑大的继续保存,更新count = 0。

  注意,最后遍历完,如果最后一段是1的话,还需要维护maxCount一次

Java Solution:

Runtime beats 70.20%

完成日期:05/10/2017

关键词:Array

关键点:维护一个maxCount

 public class Solution
{
public int findMaxConsecutiveOnes(int[] nums)
{
int maxCount = 0;
int count = 0; for(int i=0; i<nums.length; i++)
{
if(nums[i] == 1) // count consecutive ones
count++;
else if(nums[i] == 0) // if reach 0, save large count into maxCount
{
maxCount = Math.max(maxCount, count);
count = 0; // update count to 0
} }
// check the last consecutive ones
maxCount = Math.max(maxCount, count); return maxCount;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 485. Max Consecutive Ones (最长连续1)的更多相关文章

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

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

  2. [leetcode]128. Longest Consecutive Sequence最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...

  3. [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数

    题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...

  4. 8. leetcode 485. Max Consecutive Ones

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

  5. LeetCode 485 Max Consecutive Ones 解题报告

    题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...

  6. (双指针) leetcode 485. Max Consecutive Ones

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

  7. LeetCode: 485 Max Consecutive Ones(easy)

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

  8. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  9. 【leetcode】485. Max Consecutive Ones

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

随机推荐

  1. 【干货】教你如何利用fullPage.js以及move.js插件打造高端大气的网站效果!

    前言: 如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次. 在学习过jQuery插件之后,才发现之前的很多网站特效完全可 ...

  2. 全栈工程师带你开发 ,node开发人脸识别门禁系统

    效果图:       知识点: 人脸识别SKD部署,  webRTC视频流处理,URL构建blob对象,Canvas映射截图,ajax数据交互,Node图像处理,跨域与413处理,base64解码,p ...

  3. CentOs下,配置tomcat支持https

    网上此类教程一大堆,本文主要记录步骤和几个注意点. 首先,我们使用jdk的keytool生成证书.命令如下: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: ...

  4. 自定义工作流活动报错:您无法登陆系统。原因可能是您的用户记录或您所属的业务部门在Microsoft Dynamics 365中已被禁用。

    本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复265或者20170926可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...

  5. mysql error 1290 hy000:The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen' 解决方案

    如果在执行授权命令的时候报错 mysql> grant all privileges on *.* to root@"; ERROR (HY000): The MySQL server ...

  6. windows下PHP中Fatal error Call to undefined function curl_init()的解决方法

    参考官方解决方法:http://nz.php.net/manual/en/curl.installation.php 1.php安装目录下的ext文件夹下面是否有php_curl.dll文件,然后右键 ...

  7. Docker到底是什么

    简单讲docker和vm虚拟机类似,都是在同一硬件上虚拟化出多个服务器应用实例的功能,据Bottomley声称,借助经过全面调优的容器系统,你就可以在同一硬件上拥有数量比使用Xen虚拟机或KVM虚拟机 ...

  8. SQLserver学习(四)——T-SQL编程之事务、索引和视图

    今天来分享下T-SQL高级编程中的事务.索引.视图,可以和之前的SQL server系列文章结合起来. 一.事务 事务(TRANSACTION)是作为单个逻辑工作单元执行的一系列操作,这些操作作为一个 ...

  9. C-Flex 与 box布局教程

    http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html -阮一峰老师 http://www.w3cplus.com/css3/flexbox- ...

  10. 用git从github网站上下载代码的方式

    原本单击如下下载按钮即可 但有时候github异常,该按钮无效,可以使用如下方法: 1.复制url,如https://github.com/ulli-kroll/mt7610u 2.进入要存放该代码的 ...