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. [WPF自定义控件]?使用WindowChrome自定义Window Style

    原文:[WPF自定义控件]?使用WindowChrome自定义Window Style 1. 为什么要自定义Window 对稍微有点规模的桌面软件来说自定义的Window几乎是标配了,一来设计师总是克 ...

  2. object_detection/protos/*.proto: No such file or directory

    1 背景 使用TensorFlow Object Detection API的时,在object_detection/protos/中,可以看到一些proto 文件,需要使用protoc程序将这些pr ...

  3. 解决Eclipse建立Maven Web项目后找不到src/main/java资源文件夹的办法

    问题如题,明细见下图: 解决方法: 在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace ...

  4. spring中依赖注入

    理解依赖注入:参考https://blog.csdn.net/taijianyu/article/details/2338311 一.依赖注入让bean与bean之间以配置文件组织在一起,而不是以硬编 ...

  5. Easy Excel导出

    @GetMapping(value = "/down2") public void down2(HttpServletResponse response) throws Excep ...

  6. 四. (TDZ)展示性死区

    在ES6中怎么使用 var Let const ? 1.var 声明之前 2. let 声明之前 3. const声明之前

  7. python中使用xlrd、xlwt操作excel

    python 对 excel基本的操作如下: # -*- coding: utf-8 -*- import xlrd import xlwt from datetime import date,dat ...

  8. Centos--swoole平滑重启服务

    平滑重启: 已经打开的服务: 首先在server服务中为进程添加名字: /** * @param $server */ public function onStart($server) { swool ...

  9. linux查看系统内容

  10. java基础之BigInteger

    BigInteger类概述可以让超过Integer范围内的数据进行运算 构造方法 public BigInteger(String val) 成员方法: public BigInteger add(B ...