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;
    }
相关推荐
liliangcsdn7 分钟前
多空价差收益序列汇总统计的分析和代码示例
人工智能·算法·机器学习
啥都想学点的研究生14 分钟前
一篇文章讲清楚:Adaboost算法与GBDT
人工智能·算法
runningshark22 分钟前
CH-6图论基础-有向树
数据结构
旖旎夜光23 分钟前
LeetCode 525:连续数组(前缀和) —— 题解
数据结构·c++·算法·leetcode·前缀和
白狐_79831 分钟前
408 数据结构|双散列与探测覆盖率:互质判断法
数据结构
zander25840 分钟前
LeetCode 32. 最长有效括号
算法
lvwangshu1 小时前
AC 自动机
算法·字符串
shehuiyuelaiyuehao10 小时前
算法31,前缀和,可被k整除的子数组
数据结构·python·算法
203号居民12 小时前
LeetCode hot 100 — 141. 环形链表2
算法·leetcode·链表
玖玥拾12 小时前
LeetCode 202 快乐数
算法·leetcode