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. Hibernate的多对一映射

    一.创建Java工程,新建Lib文件夹,加入Hibernate和数据库(如MySql.Oracle.SqlServer等)的Jar包,创建 hibernate.cfg.xml 文件,并配置,配置项如下 ...

  2. WPF 先显示登录成功,验证成功后显示主窗口

    /// 设置显示登录窗口的方法: /// 在 App.xaml 中把这句 /// 删掉 StartupUri="MainWindow.xaml" 改为 StartupUri=&qu ...

  3. Python全栈开发:进度条

    import sys import time for i in range(31): # 清空打印内容 sys.stdout.write("\r") # 控制输出样式 sys.st ...

  4. ie中报错---不能执行已释放 Script 的代码

    我的原因:使用jquery.colorbox.js.在页面中使用弹框,对于父页面中的变量进行修改(弹框页面的js--window.parent.obj.arr= 数组;), 当弹框关闭之后,在父页面中 ...

  5. leetcode-106-从中序和后序遍历构造二叉树

    题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -& ...

  6. Docker配置JDK1.8

    1.安装Docker(菜鸟教程有) https://www.runoob.com/docker/centos-docker-install.html 2.docker下载centos镜像(用作配置jd ...

  7. php中date() 函数

    实例 格式化本地日期和时间,并返回格式化的日期字符串: <?php // Prints the day echo date("l") . "<br>&q ...

  8. webpack新手名词解释……妈妈再也不担心我看不懂webpack官方文档了

    __dirname : 在任何模块文件内部,可以使用__dirname变量获取当前模块文件所在目录的完整绝对路径. path.resolve(): 方法将一系列路径或路径段解析为绝对路径. 语法: p ...

  9. iOS之UIBezierPath贝塞尔曲线属性简介

    #import <Foundation/Foundation.h> #import <CoreGraphics/CoreGraphics.h> #import <UIKi ...

  10. IT技术博客

    博客收藏大全: 陈皓博客: 陈硕的博客: 风云的博客: 当然我在扯淡: hellogirl: 田守枝Java技术博客: 廖雪峰博客: Milo游戏开发: