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
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = 0, cur = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (i == 0 || nums[i - 1] == 1) {
cur += 1;
} else {
cur = 1;
}
// case [0, 1] res cover both cases
res = Math.max(res, cur);
}
}
return res;
}
}

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

  1. 【leetcode】485. Max Consecutive Ones

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

  2. 485. Max Consecutive Ones【easy】

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

  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. 485. Max Consecutive Ones

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

  7. 8. leetcode 485. Max Consecutive Ones

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

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

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

  9. (双指针) leetcode 485. Max Consecutive Ones

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

随机推荐

  1. Java学习十七

    学习内容: 1.Java字符串类 1.在utf-8编码下,每个汉字占三个字节 2.字符串和byte数组之间的相互转换 将字符串转换为byte数组 byte[] arrs = str.getBytes( ...

  2. 我读《DOOM启世录》——成为一个真正厉害的人

    序言 谈到游戏, 你的当然会想到几乎统治游戏市场多年的英雄联盟,你可能还会想起前段时间风头大盛的王者荣耀手游,你应该还会想起正在冲击着游戏市场的"吃鸡"类型游戏. 那么, 大家是否 ...

  3. 最小二乘拟合(scipy实现)

    Scipy库在numpy库基础上增加了众多数学,科学及工程计算中常用库函数.如线性代数,常微分方程数值求解,信号处理,图像处理,稀疏矩阵等. 如下理解通过Scipy进行最小二乘法拟合运算 最小二乘拟合 ...

  4. python 删除文件

    import os 删除文件: os.remove() 删除空目录: os.rmdir() 递归删除空目录: os.removedirs() 递归删除目录和文件(类似DOS命令DeleteTree): ...

  5. LeetCode——230. 二叉搜索树中第K小的元素

    给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = ...

  6. 【MySQL参数】-innodb_additional_mem_pool_size

    原博客:https://yq.aliyun.com/articles/32384

  7. 吴裕雄--天生自然ShellX学习笔记:Shell 传递参数

    在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以下实例我们向脚本传递三个参数, ...

  8. 吴裕雄--天生自然深度学习TensorBoard可视化:监控指标可视化

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 1. 生成变量监控信息并定义生 ...

  9. win10环境下pyinstaller打包pytorch遇到的问题及解决方案

    pytorch-python源码生成windows的应用程序(.exe),报错OSError: could not get source code Failed to execute script h ...

  10. windows下查看rabbitmq服务是否启动

    1.命令行进入rabbitmq的安装目录下: 如下图1步骤 2.输入命令 rabbitmqctl1 status 如下图2步骤 3.有时会提示报错,如步骤3 解决办法: 我的电脑 ==> 右键 ...