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 ...
随机推荐
- [5] 微信公众号开发 - 微信支付功能开发(网页JSAPI调用)
1.微信支付的流程 如下三张手机截图,我们在微信网页端看到的支付,表面上看到的是 "点击支付按钮 - 弹出支付框 - 支付成功后出现提示页面",实际上的核心处理过程是: 点击支付按 ...
- Spring注解@Qualifier
在使用Spring框架中@Autowired标签时默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个.当找不到一个匹配的 Bean ...
- JPA 注解的CascadeType属性
cascade表示级联操作,在表之间的关系映射时用到 CascadeType.MERGE级联更新:若items属性修改了那么order对象保存时同时修改items里的对象.对应EntityManage ...
- Flask-WTF 创建表单P2
表单安全 无需任何配置,FlaskForm将提供具有CSRF(Cross-site request forgery,也被称为one-click attack 或者session riding,通常缩写 ...
- Ubuntu 安装 SQL Server
SQL Server现在可以在Linux上运行了!正如微软CEO Satya Nadella说的,"Microsoft Loves Linux",既Windows 10内置的Lin ...
- Mysql 基于 Amoeba 的 读写分离
首先说明一下amoeba 跟 MySQL proxy在读写分离的使用上面的区别: 在MySQL proxy 6.0版本 上面如果想要读写分离并且 读集群.写集群 机器比较多情况下,用mysql pro ...
- vector 向量容器用法祥解
vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: ...
- 分享基于分布式Http长连接框架
第一次在博客园写文章,长期以来只是潜水中.本着不只索取,而要奉献的精神,随笔文章之. 现贡献一套长连接的框架.如下特性: 1:发布者可异步发送消息,消息如果发送失败,可重试发送,重试次数基于配置,消息 ...
- Eight hdu 1043 八数码问题 双搜
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- (转)simhash算法原理及实现
simhash是google用来处理海量文本去重的算法. google出品,你懂的. simhash最牛逼的一点就是将一个文档,最后转换成一个64位的字节,暂且称之为特征字,然后判断重复只需要判断他们 ...