485. Max Consecutive Ones
题目
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
##分析
二进制数组中最多的连续'1'的个数
##解答
###解法1:(我)每次'0'时取max,但返回需要再取一次max(12ms)
```
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int count1 = 0;
for (int i = 0; i
###解法2:每次'1'时取max,返回无需再取;遍历数组由for改为for each(9ms√)
```
public class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int count = 0;
int result = 0;
for (int num : nums){
if(num == 1){
count++;
result = Math.max(count,result);
}
else{
count = 0;
}
}
return result;
}
}
```
485. Max Consecutive Ones的更多相关文章
- 【leetcode】485. Max Consecutive Ones
problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
- 485. Max Consecutive Ones - LeetCode
Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
- 485. Max Consecutive Ones@python
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 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 (最长连续1)
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. Example 1: Input: [1, ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
随机推荐
- AutoItLibrary安装报错(robotframework)解决
官网下载地址:http://www.softpedia.com/get/Programming/Components-Libraries/AutoItLibrary.shtml Csdn下载地址:ht ...
- javascript中对数据文本格式化的思考
在实际应用场景中,我们常常需将一些数据输出成更加符合人类习惯阅读的格式. 保留小数点后面两位 在一些要求精度没有那么准确的场景下,我们可以直接通过Number.prototype.toFixed()来 ...
- 作为测试人员,我是这么报BUG的
在测试人员提需求的时候,大家经常会看到,测试员和开发一言不合就上BUG.然后开发一下就炸了,屡试不爽,招招致命. 曾经看到有个段子这么写道: 不要对程序员说,你的代码有BUG. 他的第一反应是:1.你 ...
- HTML5和CSS3
一.HTML5 HTML5 是 HTML 标准的最新演进版本. 这个术语代表了两个不同的概念:它是一个新的 HTML 语言版本包含了新的元素,属性和行为,同时包含了一系列可以被用来让 Web 站点和应 ...
- css单位总结
body的font-size:14px body第二代子元素的font-size: em: 1.2em=1.2*1.2*14px rem:1.2rem=1.2*14px 视口高度:1000px ...
- Class path contains multiple SLF4J bindings
[logback不同版本jar包] SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:fi ...
- SpringMVC:学习笔记(2)——RequestMapping及请求映射
SpringMVC--RequestMapping及请求映射 @RequestMapping 说明 Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请 ...
- this的相关介绍与用法
当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是this.因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,并 ...
- 读书笔记 effective c++ Item 8 不要让异常(exceptions)离开析构函数
1.为什么c++不喜欢析构函数抛出异常 C++并没有禁止析构函数出现异常,但是它肯定不鼓励这么做.这是有原因的,考虑下面的代码: class Widget { public: ... ~Widget( ...
- Wpf中鼠标样式的修改,作用点修改
最近,在做一个控件的鼠标样式,Ps加了插件,可以编辑生成.cur格式的图标. 可是,所有的改完以后,调试运行,结果发现自己制作的图标的作用点总是在左上角,而不是在"手形"图标的食指 ...