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;
    }
相关推荐
2401_891482171 分钟前
C++中的事件驱动编程
开发语言·c++·算法
sw1213895 分钟前
C++与Rust交互编程
开发语言·c++·算法
Huyuejia6 分钟前
self-attention代码
算法
2401_857918299 分钟前
模板编译期机器学习
开发语言·c++·算法
独自破碎E9 分钟前
【面试真题拆解】5秒内限10次HTTP接口访问,结合数据结构和算法说说你的思路
数据结构·http·面试
放飞自我的Coder12 分钟前
【动态规划解题思路】
算法·动态规划
2403_8355684713 分钟前
多平台UI框架C++开发
开发语言·c++·算法
yunyun3212316 分钟前
C++中的适配器模式
开发语言·c++·算法
AI_Ming18 分钟前
注意力机制拓展-大模型知识点(程序员转行AI大模型学习)
算法·ai编程
扶摇接北海17619 分钟前
洛谷:P5732 【深基5.习7】杨辉三角
数据结构·c++·算法