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;
    }
相关推荐
.道阻且长.39 分钟前
9.LeetCode算法习题讲解--滑动窗口--无重复字符的最长字串
算法·leetcode·职场和发展·哈希算法
ltl2 小时前
HNSW:图索引如何击败树索引
算法
曹牧3 小时前
C#:数字的定义和表示方式
算法·c#
Nil2084 小时前
leetcode 138随机链表的复制
算法·leetcode·链表
疯狂打码的少年5 小时前
【数据结构】图的遍历:深度优先搜索(DFS)
数据结构·笔记·算法·深度优先
-凌凌漆-6 小时前
【freertos】Task创建(v2)
java·开发语言·算法
Nil2087 小时前
leetcode 24两两交换链表中的节点
算法·leetcode·链表
.格子衫.7 小时前
033动态规划之状态压缩DP——算法备赛
算法·动态规划
ysa0510307 小时前
c++常用自带函数用法与注意
c++·笔记·算法