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. ps:HSB色彩模式

    前面我们已经学习过了两大色彩模式RGB和CMYK.色彩模式有很多种,但这两种是最重要和最基础的.其余的色彩模式,实际上在显示的时候都需要转换为RGB,在打印或印刷(又称为输出)的时候都需要转为CMYK ...

  2. MacOS系統Flutter打包apk

    一.打包前需要做一些基本设置的确认 1.应用名 2.权限设置 3.applicationId:应用唯一标识符 4.versionCode:版本号 5.versionName:版本名称 6.APP应用图 ...

  3. thinkphp model

    模型样板 <?php namespace app\model; use think\Db; use think\Model; class Admin extends Model { //表名 p ...

  4. 前端每日实战:86# 视频演示如何用纯 CSS 创作一个方块旋转动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gjgyWm 可交互视频 此视频是可 ...

  5. JSP和Servlet及浏览器与tomcat交互过程

    JSP与SERVLET区别 JSP在本质上就是Servlet,但是两者的创建方式不一样. JSP由HTML代码和JSP标签构成,可以方便地编写动态网页.因此在实际应用中采用Servlet来控制业务流程 ...

  6. 爬虫技术:从sougou网站访问微信公众号的过程

    一:分析过程:fidder + chrome开发者工具 1:输入nba跳转的页面,每页显示10条相关公众号的信息 2:分析网站得到每条标题的详情页链接地址在: 3,请求上图中的url,会返回一段js代 ...

  7. mysql和haproxy高可用

    这片文章主要介绍mysql+haproxy+keepalived的高可用使用. 有两种模式: 第一种:数据库宕机触发VIP漂移的高可用使用. 第二种:haproxy宕机出发VIP漂移的高可用. 这两种 ...

  8. 用Jquery方法实现的简单下滑菜单效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. stack1顺序栈

    顺序栈 #include<iostream> using namespace std; #define increasesize 10 template <class Object& ...

  10. 循序渐进实现仿QQ界面(三):界面调色与控件自绘

    本篇讲述如何进行界面调色.界面调色一般有两种方法,调色板和HSL色彩变换.调色板局限于256色,这里不采用,因此用HSL色彩变换实现.首先要了解一下什么是HSL色彩空间,完整且详尽的知识请到维基百科去 ...