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;
    }
相关推荐
全栈开发圈6 分钟前
新书速览|机器人系统开发与优化:算法、感知与控制策略
算法·目标跟踪·机器人
大大杰哥18 分钟前
leetcode hot100(1) 哈希
leetcode
爱写代码的倒霉蛋23 分钟前
2021天梯赛L2-4真题解析
数据结构·算法
hoiii18724 分钟前
基于CVX的储能调峰调频优化模型
算法
啦啦啦_999927 分钟前
KNN算法
算法
Engineer邓祥浩33 分钟前
LeetCode 热题 100 - 第1题:两数之和
算法·leetcode·职场和发展
white-persist33 分钟前
逆向入门经典题:从 IDA 反编译坑点到 Python 解题详细分析解释
c语言·开发语言·数据结构·python·算法·逆向·安全架构
炽烈小老头34 分钟前
【每天学习一点算法 2026/04/23】盛最多水的容器
学习·算法
Ailan_Anjuxi39 分钟前
手写数字识别零基础实战:基于PyTorch的CNN完整拆解
算法·图像识别
jiucaixiuyang41 分钟前
散户如何使用手机T0算法?
算法·量化·t0