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;
    }
相关推荐
文丑颜不良啊4 小时前
LeetCode3041~3060题目解析
java·leetcode
罗西的思考4 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
文丑颜不良啊5 小时前
LeetCode3101~3120题目解析
java·leetcode
kyle~9 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
牢姐与蒯10 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好11 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
电科_银尘12 小时前
【大模型算法与工程 02】线性回归与逻辑回归:从损失函数到决策边界
算法·逻辑回归·线性回归
玄昌盛不会编程13 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
文丑颜不良啊13 小时前
LeetCode3061~3080题目解析
java·leetcode
电科_银尘15 小时前
【大模型算法与工程 01】机器学习的问题定义:监督、无监督、自监督与强化学习
人工智能·算法·机器学习