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;
    }
相关推荐
AI视觉网奇6 小时前
ue 条件判断
笔记·算法·ue5
mit6.8246 小时前
sl|deque实现|缓存命中率
算法
天赐学c语言6 小时前
1.6 - 复制IP地址 && vector和list的区别
c++·算法·leecode
黎雁·泠崖6 小时前
栈与队列之队列入门攻略:从核心概念到链表实现
c语言·数据结构·链表
多米Domi0116 小时前
0x3f 第23天 黑马web (前端三件套,maven,web入门、mysql)黑马反射注解 hot100普通数组
java·python·mysql·算法·leetcode·maven
上海锟联科技6 小时前
DAS-U1000 极致版解调卡
数据结构·算法·嵌入式实时数据库
Watermelo6176 小时前
TOON:一种为大模型设计的JSON压缩型数据结构
数据结构·人工智能·语言模型·自然语言处理·数据挖掘·数据分析·json
Swift社区14 小时前
LeetCode 465 最优账单平衡
算法·leetcode·职场和发展
聆风吟º14 小时前
【数据结构手札】空间复杂度详解:概念 | 习题
java·数据结构·算法
weixin_4450547215 小时前
力扣热题51
c++·python·算法·leetcode