zoukankan      html  css  js  c++  java
  • 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.

     [暴力解法]:

    用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;
        }
    }
    View Code
  • 相关阅读:
    [转载]美国名校的与机器视觉相关的研发中心网址
    图像匹配方法浅谈
    Debug FFMpeg with CodeBlock
    zircobrowser 源代码学习 一个Android平台的浏览器,适合用在学UI
    iPhone addSubview sameview 是否会产生内存泄漏
    Android 游戏demo
    C# 多线程 注意问题 总结
    Builld with gloox
    论坛专用
    VMware vSphere Client(4.1/5.0/5.1/5.5/6.0) 客户端下载地址
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8893855.html
Copyright © 2011-2022 走看看