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的个数最大值,遇到为0的值,遍历的i重置为0

优秀代码:

int findmax(vector<int>& nums){
int m = , MAX= ;
for(auto n : nums){
if(n == ){
MAX = max(MAX, m);
m = ;
}
else
m++;
}
return max(MAx, m);
}

本人代码:(不简洁)

int findmax(vector<int>& nums){
int m = ; max = ;
for(size_t i = ; i < nums.size(); i++){
if(nums[i] == )
m++;
if((i < nums.size() - && nums[i+] == ) || i == nums.size() - ){//当没有遍历到数组末尾,下一个为0,或者已经到了末尾,没有下一个
if(m > max)
max = m;
m = ;
}
}
}

[Array]485. Max Consecutive Ones的更多相关文章

  1. 485. Max Consecutive Ones【easy】

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

  2. 【leetcode】485. Max Consecutive Ones

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

  3. 485. Max Consecutive Ones - LeetCode

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

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

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

  5. 485. Max Consecutive Ones@python

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

  6. LeetCode Array Easy 485. Max Consecutive Ones

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

  7. 485. Max Consecutive Ones

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

  8. 8. leetcode 485. Max Consecutive Ones

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

  9. LeetCode 485. Max Consecutive Ones (最长连续1)

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

随机推荐

  1. UNION操作用于合并两个或多个 SELECT 语句的结果集。

    UNION操作用于合并两个或多个 SELECT 语句的结果集. 大理石平台价格 使用示例: $Model->field('name') ->table('think_user_0') -& ...

  2. SpringData_04_ JPA中的一对多

    1.JPA中的一对多 在一对多关系中,我们习惯把一的一方称之为主表,把多的一方称之为从表.在数据库中建立一对多的关系,需要使用数据库的外键约束. 什么是外键? 指的是从表中有一列,取值参照主表的主键, ...

  3. python中的多线程编程与暂停、播放音频的结合

    先给两个原文链接: https://blog.csdn.net/u013755307/article/details/19913655 https://www.cnblogs.com/scolia/p ...

  4. 转:链表相交有环 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯

    源地址:http://blog.csdn.net/sj13051180/article/details/6754228 1.判断单链表是否有环,要求空间尽量少(2011年MTK) 如何找出环的连接点在 ...

  5. Python PIL 怎么知道写入图片格式的kb大小

    把图片数据写入一个IO,读这个IO的长度大小: #-*-coding:utf-8-*- from PIL import Image import io img = Image.open("1 ...

  6. 732F Tourist Reform

    // CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...

  7. 在 Angularjs 中$state.go 如何传递参数

    在目标页面规定接受的参数: .state('app.AttendanceEditFixed', { url: '/AttendanceEditFixed', params: {'id': null,' ...

  8. 1008-Redo

    关于flag,都立下了 T1 考试的时候就觉得是贪心,但是不会反悔emm…… 于是正解就是一个堆优化可反悔的贪心=.= 每次找前面最小的,于是是小根堆. 我们在交易的时候发现后面的一个可以更优. 于是 ...

  9. skyline(TG,arcgis server)BS系统部署

    skyline的BS系统部署,正常情况下应该是TG来统一管理,SFS对矢量数据服务进行管理.但我们一直是试用许可安装的TG,发现SFS要么安装不成功,要么就是不稳定.对于Fly工程可以通过Publis ...

  10. 玩转python爬虫之正则表达式

    玩转python爬虫之正则表达式 这篇文章主要介绍了python爬虫的正则表达式,正则表达式在Python爬虫是必不可少的神兵利器,本文整理了Python中的正则表达式的相关内容,感兴趣的小伙伴们可以 ...