[抄题]:

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.

[暴力解法]:

用temp,result,结果发现写起来很麻烦

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

还是局部最大值+全局最大值的思路。用三元表达式解决局部变量的连续和中断问题

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 是maxHere + 1 统计,不是n + 1,这是是乐至?

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

用三元表达式解决局部变量的连续和中断问题

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

487. Max Consecutive Ones II 好像不是数学就是两根指针吧

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return 0;
} //ini
int max = 0;
int maxHere = 0; //for loop
for (int n : nums) {
max = Math.max(max, maxHere = n == 0 ? 0 : maxHere + 1);
} //return
return max;
}
}

485. Max Consecutive Ones最长的连续1的个数的更多相关文章

  1. [LeetCode]485. Max Consecutive Ones 找到最大的连续的1的个数

    题目描述 输入只有0和1的数组(长度为正整数,且<10000),找到最大的连续1的个数 比如[1,1,0,1,1,1],输出3 思路 遍历数组,统计当前连续个数curCount和最大连续值max ...

  2. 485. Max Consecutive Ones - LeetCode

    Question 485. Max Consecutive Ones Solution 题目大意:给一个数组,取连续1的最大长度 思路:遍历数组,连续1就加1,取最大 Java实现: public i ...

  3. 【leetcode】485. Max Consecutive Ones

    problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vec ...

  4. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  5. 485 Max Consecutive Ones 最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意:    输入的数组只包 ...

  6. 485. Max Consecutive Ones

    题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...

  7. 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 (最长连续1)

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  9. 485. Max Consecutive Ones最大连续1的个数

    网址:https://leetcode.com/problems/max-consecutive-ones/ 很简单的一题 class Solution { public: int findMaxCo ...

随机推荐

  1. DEV控件 皮肤问题

    今天用cnPack清理了下整个工程的引用单元,清理完,问题来了,TcxPageControl不透明了. 折腾了一会,找到原因,清理单元时将dxSkinscxPCPainter也清掉了,导致皮肤无法正常 ...

  2. Java程序员必须掌握的知识

    1.语法:Java程序员必须比较熟悉语法,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息 知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项, ...

  3. SqlServer 数据库负载均衡【转】

    负载均衡集群是由一组相互独立的计算机系统构成,通过常规网络或专用网络进行连接,由路由器衔接在一起,各节点相互协作.共同负载.均衡压力,对客户端来说,整个群集可以视为一台具有超高性能的独立服务器. 1. ...

  4. 快速沃尔什变换FWT

    快速沃尔什变换\(FWT\) 是一种可以快速完成集合卷积的算法. 什么是集合卷积啊? 集合卷积就是在集合运算下的卷积.比如一般而言我们算的卷积都是\(C_i=\sum_{j+k=i}A_j*B_k\) ...

  5. 在Spring中通过构造自动装配--constructor

    在Spring中,可以使用“通过构造自动装配”,实际上是按构造函数的参数类型自动装配. 这意味着,如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么将自动装配. packag ...

  6. C++输入流和输出流、缓冲区

    一.C++输入流和输出流 输入和输出的概念是相对程序而言的. 键盘输入数据到程序叫标准输入,程序数据输出到显示器叫标准输出,标准输入和标准输出统称为标准I/O,文件的输入和输出叫文件I/O. cout ...

  7. (转)Android之Adapter用法总结

    1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ...

  8. mysql链接出现10060

    http://www.cnblogs.com/meetrice/p/5309666.html 使用navicate链接 http://www.cnblogs.com/cxint/p/7454054.h ...

  9. nginx.conf几个示例

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  10. fineUI表格控件各属性说明

    最近在新的公司后台用fineUI,所以也稍微总结一下表格的一些属性,希望对刚入手的人有些帮助: AllowPaging 允许分页,为true时表示允许分页,表格下方会出现分页工具栏: PageSize ...