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,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
题目标签:Array
题目给了我们一个nums array, 里面只会出现0和1, 让我们找到一个最长连续1的长度。
这题很简单,只要维护一个maxCount 就可以了,再设一个count,遇到1的时候,count++;遇到0的时候,把count 和 maxCount 中挑大的继续保存,更新count = 0。
注意,最后遍历完,如果最后一段是1的话,还需要维护maxCount一次
Java Solution:
Runtime beats 70.20%
完成日期:05/10/2017
关键词:Array
关键点:维护一个maxCount
public class Solution
{
public int findMaxConsecutiveOnes(int[] nums)
{
int maxCount = 0;
int count = 0; for(int i=0; i<nums.length; i++)
{
if(nums[i] == 1) // count consecutive ones
count++;
else if(nums[i] == 0) // if reach 0, save large count into maxCount
{
maxCount = Math.max(maxCount, count);
count = 0; // update count to 0
} }
// check the last consecutive ones
maxCount = Math.max(maxCount, count); return maxCount;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 485. Max Consecutive Ones (最长连续1)的更多相关文章
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- [leetcode]128. Longest Consecutive Sequence最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Y ...
- [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数
题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- (双指针) leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- LeetCode: 485 Max Consecutive Ones(easy)
题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
随机推荐
- Signal ()函数详细介绍 Linux函数(转)
Signal ()函数详细介绍 Linux函数 收藏人:紫火神兵 2012-09-27 | 阅:5659 转:22 | 来源 | 分享 signa ...
- 在windows下安装flex和bison
学习Stellar-core 需要依赖项flex .bison .gcc三个依赖项 下载得网址:链接: https://pan.baidu.com/s/1mitCLcs 密码: 3jaj 通过 w ...
- Struts2配置文件复用代码【web.xml、struts.xml、常量配置】
web.xml的分发器代码: <!-- 引入struts核心过滤器 --> <filter> <filter-name>struts2</filter-nam ...
- Intellij idea插入表数据【在UI界面插入出错】
使用Intellij idea向数据库插入表数据的时候,如果该表是联合主键的,那么不能使用UI界面来进行插入-- 必须通过SQL语句才能插入-- 至于为什么?我也不知道-.搞了大半天--想省时间不写S ...
- Java main方法继承
java中main方法是可以继承的 Test1.java package Variables; public class Test1 { public static void main(String[ ...
- jquery ocupload一键上传文件应用
直接上栗子 这是官方文档栗子 var myUpload = $(element).upload({ name: 'file', action: '', enctype: 'multipart/form ...
- 再起航,我的学习笔记之JavaScript设计模式25(迭代器模式)
迭代器模式 概念介绍 迭代器模式(Iterator): 在不暴露对象内部结构的同时,可以顺序地访问聚合对象内部的元素. 迭代器 程序中的循环是一种利器,循环语句也使我们程序开发更简洁高效,但是有时一遍 ...
- java集合系列——List集合之Stack介绍(五)
1.Stack的简介 Stack 类表示后进先出(LIFO)的对象堆栈.它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈.它提供了通常的 push 和 pop 操作,以及取堆栈顶点的 ...
- [Tjoi2013]循环格
[Tjoi2013]循环格 2014年3月18日1,7500 Description Input 第一行两个整数R,C.表示行和列,接下来R行,每行C个字符LRUD,表示左右上下. Output 一个 ...
- 简单Elixir游戏服设计-玩家进程注册
上回说用Registry 做本地注册(跨服可以用syn,只是稍微麻烦点,需要模拟global注册机制,写个封装模块). 修改game_server 项目的mix.exs, 增加应用启动 def app ...