1、题目

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

2、代码实现

public class Solution {public int findMaxConsecutiveOnes(int[] nums) {if (nums == null) {return 0;}int zero = 0;for (int i = 0; i < nums.length; i++) {if (nums[i] == 0) {++zero;}}if (zero == nums.length) {return 0;}int count = 0;int maxValue = 0;int maxLength = 0;for (int i = 0; i < nums.length; ++i) {if (nums[i] > maxValue) {maxValue = nums[i];}}for (int i = 0; i < nums.length; ++i) {if (nums[i] == maxValue) {for (int j = i; j < nums.length; ++j) {if (nums[j] == maxValue) {count++;} else {break;}}if (maxLength < count) {maxLength = count;}count = 0;}}return maxLength;}
}

LeetCode之Max Consecutive Ones相关推荐

  1. LeetCode 485. Max Consecutive Ones

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

  2. leetcode 485,487,1004. Max Consecutive Ones I ,II, III(最大连续1的个数问题合集)

    485. Max Consecutive Ones https://leetcode.com/problems/max-consecutive-ones/ easy 题,思路不说了,直接上代码. cl ...

  3. 485. Max Consecutive Ones - LeetCode

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

  4. Leetcode日练笔记19 #487 Max Consecutive Ones II (Medium)

    #487 Max Consecutive Ones II (Medium) Given a binary array nums, return the maximum number of consec ...

  5. 【重要+细节】LeetCode 149. Max Points on a Line

    LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcod ...

  6. LeetCode 695. Max Area of Island

    LeetCode 695. Max Area of Island Given a non-empty 2D array grid of 0's and 1's, an island is a grou ...

  7. C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3714 访问. 给定一个二进制数组, 计算其中最大连续1的个数. ...

  8. 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  9. 【Leetcode】487. Max Consecutive Ones II

    题目地址: https://leetcode.com/problems/max-consecutive-ones-ii/description/ 给定一个长nnn的010101数组AAA,允许将任意一 ...

最新文章

  1. include和require的区别
  2. numpy.savetxt()保存两列
  3. Liunx UID and GID
  4. phpmyadmin不允许一个表创建多个主键的解决办法
  5. 如何制作Windows镜像
  6. JavaScript权威指南--多媒体和图形编程
  7. mybitis实现增,删,改,查,模糊查询的两种方式:(2)
  8. Pl/SQL Developer 配置
  9. javascript Blob数据解析 HUOBI火币api数据解析
  10. 1.12 梯度的数值逼近
  11. [渝粤教育] 西南科技大学 刑事诉讼法学 在线考试复习资料(1)
  12. 高性能diffpatch算法 -- 如何将微信Apk的官方增量包20.4M缩小到7.0M
  13. 你来分我先选 原则
  14. 基于LSTM神经网络的负荷预测(Python代码实现)
  15. python全栈开发包括那些_什么是全栈工程师?有哪些知识?
  16. 苹果如何安装ipa(亲测有用,无需越狱)
  17. tuts4you 注册_设计和开发完整的网站(Tuts + Mini系列,第3页)
  18. 暖暖环游世界显示服务器异常,暖暖环游世界客服常见问题汇总
  19. 2019年—可能是我们Java程序员离危机感最近的一年!
  20. 12C DELETE FROM wri$_adv_sqlt_rtn_planWHERE task_id = :tid AND exec_name = :execution_name

热门文章

  1. 鹅厂二面,Nginx回忆录
  2. 项目开发中经常有一些被嫌弃的小数据,现在全丢给 FastDFS
  3. 双城生活,一种相对无奈且幸福的选择
  4. 微服务架构下的测试策略
  5. 我的 .NET Core 博客性能优化经验总结
  6. 【译】gRPC vs HTTP APIs
  7. 集群环境下,你不得不注意的ASP.NET Core Data Protection 机制
  8. 你的通勤时间都去哪了?
  9. .NET Core 3.0 可卸载程序集原理简析
  10. .net测试篇之单元测试/集成测试神器Autofixture