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;
    }
相关推荐
CN-Dust1 小时前
【C++】while语句例题专题
数据结构·c++·算法
灵智实验室1 小时前
PX4位置速度估计技术详解(四):LPE 激光雷达高度融合的实现错误
算法·无人机·px 4
CQU_JIAKE1 小时前
【A】3742,3387,并查集
算法
gihigo19981 小时前
CHAN时延估计算法(二维/三维定位实现)
算法
freexyn2 小时前
Matlab自学笔记七十六:表达式的展开、因式分解、化简、合并同类项
笔记·算法·matlab
样例过了就是过了2 小时前
LeetCode热题 不同路径
c++·算法·leetcode·动态规划
dog2502 小时前
圆锥曲线和二次曲线
开发语言·网络·人工智能·算法·php
Wadli2 小时前
27.单调队列
算法
Navigator_Z2 小时前
LeetCode //C - 1031. Maximum Sum of Two Non-Overlapping Subarrays
c语言·算法·leetcode
Wect3 小时前
LeetCode 97. 交错字符串:动态规划详解
前端·算法·typescript