给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: [1,1,0,1,1,1]
输出: 3
解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.
注意:
    输入的数组只包含 0 和1。
    输入数组的长度是正整数,且不超过 10,000。
详见:https://leetcode.com/problems/max-consecutive-ones/description/

Java实现:

class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int size=nums.length;
if(size==0||nums==null){
return 0;
}
int cnt=0;
int res=0;
for(int num:nums){
cnt=num==0?0:cnt+1;
res=Math.max(res,cnt);
}
return res;
}
}

C++实现:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int cnt=0,res=0;
for(int num:nums)
{
cnt=num==0?0:cnt+1;
res=max(res,cnt);
}
return res;
}
};

485 Max Consecutive Ones 最大连续1的个数的更多相关文章

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

    网址:https://leetcode.com/problems/max-consecutive-ones/ 很简单的一题 class Solution { public: int findMaxCo ...

  2. [LeetCode] Max Consecutive Ones 最大连续1的个数

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

  3. Leetcode485.Max Consecutive Ones最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...

  4. 485. Max Consecutive Ones - LeetCode

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

  5. 【leetcode】485. Max Consecutive Ones

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

  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. 485. Max Consecutive Ones最长的连续1的个数

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

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

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

  9. 485. Max Consecutive Ones

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

随机推荐

  1. 文件管理中心iOS APP (国外市场:File Center) 技术支持

    文件管理中心iOS APP (国外市场:File Center) 技术支持网址:http://www.cnblogs.com/flychen/邮箱:592802944@qq.com

  2. js replace()实现全部替换

    var r= "1\n2\n3\n"; //将字母\n替换成分号 alert(r.replace("\n",";")); 结果:1;2\n3 ...

  3. 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)

    刘汝佳:<训练指南>Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h&g ...

  4. html5--6-23 CSS3中的文字与字体

    html5--6-23 CSS3中的文字与字体 text-overflow 设置是否使用一个省略标记(...)标示对象内文本的溢出 clip: 默认值当对象内文本溢出时不显示省略标记(...),而是将 ...

  5. iOS设备闪光灯控制

    很多时候都需要在APP中控制闪光灯的开关状态,譬如扫描二维码.控制iOS设备的闪光灯代码非常简单,短短几行代码就可以搞定: AVCaptureDevice *device = [AVCaptureDe ...

  6. macbook pro 配置jdk,maven环境变量

    https://blog.csdn.net/vvv_110/article/details/72897142(1)下载jdk安装包,进行安装,maven安装包,进行解压 (2)改写环境变量的配置文件/ ...

  7. NOIP2007 矩阵取数游戏(区间DP)

    传送门 这道题第一眼看上去可能让人以为是贪心……不过贪心并不行,因为每次的操作是有2的幂次方的权值的.这样的话直接每次贪心最小的就目光短浅.所以那我们自然想到了DP. 据说这是一道很正常的区间DP? ...

  8. Memcached使用总结之:使用Python操作memcache

    Python连接memcached的库有很多,处于简单以及高效的原则,最终选择了pymemcache,优点完全实现了memcached text协议对于send/recv操作可以配置timeout支持 ...

  9. 多线程-threading模块3

    超级播放器 #coding:utf-8 import threading from time import sleep,ctime #超级播放器 def super_player(file,time) ...

  10. 国产免费的visio替代品edraw mind map,用来话流程图够用了

    最新版Edraw Mind Map可以创建基本的思维导图.气泡图和基本流程图,提供了强大的设计功能,包括丰富设计素材.全面的页面布局定义.预置的符号库与绘图工具等.创建的图形,可以导出为常用图像格式. ...