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. springboot通过继承OncePerRequestFilter,在拦截器中@Autowired 为null问题

    springboot2版本以上环境 通过继承OncePerRequestFilter类,在重写doFilterInternal方法实现拦截的具体业务逻辑, 在实现的过程中,需要注入service方法, ...

  2. hive之wordcount

    1.创建一张表,记录文件数据,使用换行符作为分隔符 create table file_data(content string) row format delimited fields termina ...

  3. voc数据集坐标,coco数据集坐标

    voc,如上图 x1 ,y1 ,x4, y4    bbox的坐标格式是,x,y的最大最小值,也就是box的左上角和右下角的坐标 coco x,y,w,h       box左上角的坐标以及宽.高 图 ...

  4. DELPHI 调试IOS时出现 Please specify exact device preset UUID

    右击设备DEVICE, 选择REFRESH刷新, 再重新选择一个设备即可.

  5. SSM项目web.xml等配置文件中如何查找类的全路径名?

    如题, web.xml,applicationContext.xml 等配置文件中,有时不会出现自动提示类的名字,这时如何查找类的全路径名,如下图所示: 1.鼠标右键单击菜单栏Navigate选项,选 ...

  6. Sentinel 1.7.0 发布,支持 Envoy 集群流量控制

    流控降级中间件Sentinel 1.7.0版本正式发布,引入了 Envoy 集群流量控制支持.properties 文件配置.Consul/Etcd/Spring Cloud Config 动态数据源 ...

  7. django的安装和初步使用

    安装参考:步骤也可以参考这个 很详细 https://blog.csdn.net/zww1984774346/article/details/54408759 如果想在终端查看项目结构 需要用到tre ...

  8. android设置系统默认开机时间

    1.设置RTC时间,该时间是如果RCT时钟断电以后使用的默认时间 Android L之前: \alps\mediatek\custom\[project]\preloader\ inc\cust_rt ...

  9. Spring JDBCTemplate 简单使用

    Spring JDBCTemplate applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8&q ...

  10. Python 数字系列-数字格式化输出

    数字的格式化输出 问题 你需要将数字格式化后输出,并控制数字的位数.对齐.千位分隔符和其他的细节. 解决方案 格式化输出单个数字的时候,可以使用内置的 format() 函数,比如: >> ...