LeetCode之Max Consecutive Ones
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
and1
. - 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相关推荐
- LeetCode 485. Max Consecutive Ones
题目描述: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- leetcode 485,487,1004. Max Consecutive Ones I ,II, III(最大连续1的个数问题合集)
485. Max Consecutive Ones https://leetcode.com/problems/max-consecutive-ones/ easy 题,思路不说了,直接上代码. cl ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 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 ...
- 【重要+细节】LeetCode 149. Max Points on a Line
LeetCode 149. Max Points on a Line Solution1: 参考花花酱:https://zxi.mytechroad.com/blog/geometry/leetcod ...
- 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 ...
- C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3714 访问. 给定一个二进制数组, 计算其中最大连续1的个数. ...
- 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 【Leetcode】487. Max Consecutive Ones II
题目地址: https://leetcode.com/problems/max-consecutive-ones-ii/description/ 给定一个长nnn的010101数组AAA,允许将任意一 ...
最新文章
- include和require的区别
- numpy.savetxt()保存两列
- Liunx UID and GID
- phpmyadmin不允许一个表创建多个主键的解决办法
- 如何制作Windows镜像
- JavaScript权威指南--多媒体和图形编程
- mybitis实现增,删,改,查,模糊查询的两种方式:(2)
- Pl/SQL Developer 配置
- javascript Blob数据解析 HUOBI火币api数据解析
- 1.12 梯度的数值逼近
- [渝粤教育] 西南科技大学 刑事诉讼法学 在线考试复习资料(1)
- 高性能diffpatch算法 -- 如何将微信Apk的官方增量包20.4M缩小到7.0M
- 你来分我先选 原则
- 基于LSTM神经网络的负荷预测(Python代码实现)
- python全栈开发包括那些_什么是全栈工程师?有哪些知识?
- 苹果如何安装ipa(亲测有用,无需越狱)
- tuts4you 注册_设计和开发完整的网站(Tuts + Mini系列,第3页)
- 暖暖环游世界显示服务器异常,暖暖环游世界客服常见问题汇总
- 2019年—可能是我们Java程序员离危机感最近的一年!
- 12C DELETE FROM wri$_adv_sqlt_rtn_planWHERE task_id = :tid AND exec_name = :execution_name