给定一个二进制数组, 计算其中最大连续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. Struts2的运行流程及其工作原理

    1 服务开启,配置文件初始化 2 用户访问login请求 3 进入web.xml文件中我们配置的核心控制器(filter) 4 核心过滤器中有一个FilterDispatcher,FilterDisp ...

  2. Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry

    B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. js中!~什么意思

    (function () { var names = []; return function (name) { addName(name); } function addName(name) { if ...

  4. 基于C#实现Windows服务状态启动和停止服务的方法

    网址:http://www.jb51.net/article/89230.htm

  5. 百度地图API--信息窗口

    信息窗口 -----纯文本信息窗口 为了更方便的提示用户,在地图的指定的地方添加文本信息的窗口,给用户直观展示信息,下面是如何添加一个纯文本的信息窗口. 下面是添加一个文本窗口: /* * 添加纯文本 ...

  6. 屏幕适配-使用autoLayout

    当遇见xib中无法删除的控件时. 将这个错误的控件拖离本xib(第一个元素.xib文件是有许多元素组成的集合),确保这个xib是正确的.重新创建一个xib文件,将这个正确的xib元素整个复制过去. 在 ...

  7. BroadcastReceiver中调用Service

    首先是代码: package com.larry.msglighter; import android.content.BroadcastReceiver; import android.conten ...

  8. Eclipse中文乱码问题

    最近用TortoiseSVN从androiddex上下载了一个android工程,导入之后发现里面的中文都是乱码,于是在Eclipse里通过Windows-Preferences-Workspace修 ...

  9. sublime text 3 安装vue 语法插件

    1.下载https://github.com/vuejs/vue-syntax-highlight,点击这里也可以下载压缩包 2.解压到C:\Users\***\AppData\Roaming\Sub ...

  10. In-App Purchase Programming Guide----(四) ----Requesting Payment

    Requesting Payment In the second part of the purchase process, after the user has chosen to purchase ...