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数组 ...
随机推荐
- AIX上面Oracle数据库相关启动
1,启动停止Oracle实例 (1) su -oracle (2) echo $ORACLE_SID (3) sqlplus /nolog //以不登录到数据库的方式进入sqlplus环境 (4) c ...
- PHP扩展之多线程
PHP一直以为不支持多线程,后面才知道有基于pThread的扩展包,地址如下: http://php.net/manual/zh/book.pthreads.php 我感兴趣的是以下几个点: 1.Th ...
- Ubuntu14.04安装Go语言开发环境
1.使用apt-get命令来安装Go环境 apt-get install software-properties-common apt-get install python-software-prop ...
- Hadoop权威指南: InputFormat,RecordReader,OutputFormat和RecordWriter
InputFormat和RecordReader Hadoop提出了InputFormat的概念 org.apache.hadoop.mapreduce包里的InputFormat抽象类提供了如下列代 ...
- java异常的一些小知识
异常,我们软件都需要面对的一个问题.如何让你的软件更加健壮呢?这是一个值得我们考虑的问题.这里主要为大家介绍一下异常是什么,异常是如何产生的,如何将异常抛出,如何捕获异常,对于异常应该如何处理的个人一 ...
- centos 7安装es 及异常处理
首先,我们从官网下载zip包:(官网:https://www.elastic.co/downloads/elasticsearch) 直接使用浏览器下载可能会很慢,我一般会copy下载链接,然后w ...
- Java String类和Object类
String类: 方法: 1.charAt(int index):取index下标的char类型值 2.endsWith(String prefix) /startsWith(String prefi ...
- wordPress查看站点时,显示文件目录
1.在wordpress的代码目录中增加.htaccess文件. 2.在.htaccess文件中加入如下内容: DirectoryIndex index.php index.html# BEGIN W ...
- jQuery_第三章_工厂函数
- Codeforces Round #372 +#373 部分题解
用了两场比赛上Div 1感觉自己好腊鸡的说...以下是这两场比赛的部分题解(不得不说有个黄学长来抱大腿还是非常爽的) Round #372 : Div 2 A:Crazy Computer 题意:给定 ...