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. Python说文解字_杂谈04

    1. 鸭子类型: 当你看到一只鸟走来像鸭子,游泳起来像鸭子,叫起来也像鸭子,他么他就可以叫做鸭子.任何可迭代的对象.一样的方法,可以用可迭代的话,就可以迭代的组合打印.__getitem__可以塞到任 ...

  2. $_SESSION $_COOKIE

    $_SESSION是临时会话变量,用来储存访问者信息.内容是储存在服务器上面的.比如 $_SESSION["ABC"] = "aaa";那么这个用户访问时,$_ ...

  3. 三大PLM厂商

    西门子的叫Teamcenter(汽车.通用机械) 法国达索的Enovia(航空\汽车\高铁等交通运输行业和机械行业) PTC的叫windchill(船舶.电子)

  4. 13)编写一个子类SnakeCtrl来继承基类

    1)首先是创建子类SnakeCtrl 2)     添加那个SnakeCtrl子类 3)出来了SnakeCtrl的基本样子 4)简单实现: ①改编那个SnakeCtrl.h中的内容: #pragma ...

  5. Ubuntu---VIM 常用命令

    今天学习 VIM 的一些常用命令,向传说中的“最后一个编辑器”进攻,哈哈 插入命令: # insert i : 当前光标之前插入 I : 在此行的行首插入 o : 在下一行新起一行插入 O : 在上一 ...

  6. PAT Basic 1083 是否存在相等的差 (20) [hash映射,map STL]

    题目 给定 N 张卡⽚,正⾯分别写上 1.2.--.N,然后全部翻⾯,洗牌,在背⾯分别写上 1.2.--. N.将每张牌的正反两⾯数字相减(⼤减⼩),得到 N 个⾮负差值,其中是否存在相等的差? 输⼊ ...

  7. 寒假day06

    今天完善了毕设的数据抽取功能,新增了几点: 1.已经抽取过的表由系统给出相应提示 2.生成数据抽取记录并展示 3.界面优化

  8. MySQL去除表里数据回车符,换行符,空格和水平制表符

    MySQL去除表里数据回车符,换行符,空格和水平制表符 最近导数据的时候发现表里有好多回车符,换行符,水平制表符,MySQL的trim函数没办法去掉回车和换行,只能去掉多余的空格,可以用MySQL的r ...

  9. 复杂json解析方式[GsonFormat]

    针对开发人员来讲,善于用工具,事半功倍. 干货: 1.IntelliJ IDEA 通过GsonFormat插件将JSONObject格式的String 解析成实体 插件地址:https://plugi ...

  10. Opencv笔记(一):图像的基本操作

    1.图像的读.显示和保存操作 import cv2 img = cv2.imread('filename',0); cv2.imshow('name',img); k=cv2.waitKey(0); ...