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;
    }
相关推荐
happyprince41 分钟前
03_NVIDIA_ModelOpt-量化算法深入
人工智能·深度学习·算法
大鱼>1 小时前
AI+货物追踪:智能快递柜追踪系统
人工智能·深度学习·算法·机器学习
researcher-Jiang1 小时前
算法训练:堆 & 可并堆
算法
在书中成长2 小时前
HarmonyOS 小游戏《对战五子棋》开发第18篇 - 棋盘设计
算法·harmonyos
Frostnova丶2 小时前
(12)LeetCode 76. 最小覆盖子串
算法·leetcode·职场和发展
灯澜忆梦2 小时前
GO_函数_1
算法
小七在进步2 小时前
数据结构:栈与队列之栈的实现
数据结构
言乐62 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
凉云生烟3 小时前
机器学习 02- KNN算法
人工智能·算法·机器学习
wabs6663 小时前
关于动态规划【力扣583.两个字符串的删除操作的思考】
算法·leetcode·动态规划