leetcode最大连续1的个数(简单)

比较简单,但解时候如果能一次通过更好

方案一

java 复制代码
    public int findMaxConsecutiveOnes(int[] nums) {
        int i = -1,j = 0,max = 0;
        while(j < nums.length && max < nums.length - i) {
            if(nums[j] != 1) {
                i = j;
            } else if(max < j - i){
                max = j - i;
            }
            j++;
        }
        return max;
    }

方案二

java 复制代码
    public int findMaxConsecutiveOnes(int[] nums) {
        int i = -1,count = 0,max = 0;
        while(++i < nums.length) {
            if(nums[i] == 0) {
                count = 0;
            } else {
                count++;
            }
            if(max < count) max = count; 
        }
        return max;
    }
相关推荐
hrrrrb4 分钟前
【算法设计与分析】贪心算法
算法·贪心算法·代理模式
TracyCoder12316 分钟前
LeetCode Hot100(10/100)—— 53. 最大子数组和
算法·leetcode
Σίσυφος19001 小时前
霍夫变换vs LS vs RANSAC 拟合直线 MATLAB实现
算法·计算机视觉·matlab
闲人不梦卿1 小时前
数组和矩阵以及广义表
数据结构
假女吖☌1 小时前
限流算法-redis实现与java实现
java·redis·算法
蒟蒻的贤1 小时前
两数之和。
算法
wen__xvn1 小时前
代码随想录算法训练营DAY27第八章 贪心算法 part01
算法·贪心算法
We་ct2 小时前
LeetCode 125. 验证回文串:双指针解法全解析与优化
前端·算法·leetcode·typescript
客卿1232 小时前
力扣20-有效括号(多家面试题)
算法·leetcode·职场和发展
木井巳2 小时前
【递归算法】快速幂解决 pow(x,n)
java·算法·leetcode·深度优先