Description

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

Example 1:

Input: [,,,,,]
Output:
Explanation: The first two digits or the last three digits are consecutive 1s.
The maximum number of consecutive 1s is .

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连续出现的最多次数

我的思路:定义两个变量,一个counter 记录出现次数,一个temp 记录当前出现次数, 如果遇0则temp归零重新计数。同时考虑边界情况

public int FindMaxConsecutiveOnes(int[] nums) {
if(nums.Length == )
return ;
int counter = ;
int temp = ;
for(int i = ; i < nums.Length; i++){
if(nums[i] == )
{
temp++;
}else
{
if(temp > counter)
{
counter = temp;
}
temp = ;
}
}
if(temp > counter)
{
counter = temp;
} return counter;
}

LeetCode Array Easy 485. Max Consecutive Ones的更多相关文章

  1. [LeetCode&Python] Problem 485. Max Consecutive Ones

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

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

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

  4. 485. Max Consecutive Ones - LeetCode

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

  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最长的连续1的个数

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

  7. LeetCode: 485 Max Consecutive Ones(easy)

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

  8. 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  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. 牛客ACM赛 B [小a的旅行计划 ]

    链接 B 小a的旅行计划 把\(n\)个数中选任意数分成\(a,b\)两个集合,集合无区别,要求不包含且有交,求方案数.\(n\leq 10^{13}\) 首先讨论\(a,b\)并集是否为全集: 若是 ...

  2. springboot2.0整合redis的发布和订阅

    1.Maven引用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  3. Jenkins配置git/github 插件的ssh key

    参考来源:http://jingyan.baidu.com/article/a65957f4f0acc624e67f9bc1.html 方式一:本地需要生成公私钥文件,git/github中新建ssh ...

  4. CABasicAnimation来做心跳动画

    CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale"; ...

  5. hibernate插入中文字段时,无法插入数据库

    体现: hibernate插入数据时,一直报错: Caused by: java.sql.SQLException: Incorrect string value: '\xE5\xBC\xA0\xE4 ...

  6. Python_013(面向对象概念)

    一.面向对象 1.面向对象几个概念问题: a:类:具有相同属性和技能的一类事物.用代码表示就是,我类里面有一些静态变量和方法是大家共有的; b:对象:具体的类的表现.在代码中就是,调用类的方法或变量传 ...

  7. 初次打开jenkins页面一片空白的解决办法

    安装完成jenkins后,点击[使用admin账号继续]后,页面一片空白的解决办法: step1: Jenkins插件管理-高级设置界面==> http://localhost:8080/plu ...

  8. v-text、v-html、v-bind、v-show

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 高德WMTS规则

    新版 http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1& ...

  10. HTML表格<tr>行距调整

    CSS文件中: .myTable tr{     display:block; /*将tr设置为块体元素*/     margin-bottom:5px;}