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;
    }
}

原文地址:https://www.cnblogs.com/immiao0319/p/8893855.html

时间: 2024-11-08 22:33:30

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

485. Max Consecutive Ones (最大连续数) by Python

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 o

【leetcode】485. Max Consecutive Ones

problem 485. Max Consecutive Ones solution1: class Solution { public: int findMaxConsecutiveOnes(vector<int>& nums) { if(nums.empty()) return 0; int ans = 0, max = INT_MIN; for(int i=0; i<nums.size(); i++) { if(nums[i]==1) ans++; else if(nums

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

给定一个二进制数组, 计算其中最大连续1的个数.示例 1:输入: [1,1,0,1,1,1]输出: 3解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3.注意:    输入的数组只包含 0 和1.    输入数组的长度是正整数,且不超过 10,000.详见:https://leetcode.com/problems/max-consecutive-ones/description/ C++: class Solution { public: int findMaxConsecu

[LeetCode OJ]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: T

[Array]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: T

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: T

LeetCode_485. Max Consecutive Ones

485. Max Consecutive Ones Easy 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

[Leetcode] Longest consecutive sequence 最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4. Your algorithm should run in O(n

[Swift]LeetCode485. 最大连续1的个数 | 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: