力扣 485. 最大连续 1 的个数 难度:简单

题目地址:https://leetcode-cn.com/problems/max-consecutive-ones/

我的题解:

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int max = 0;
        int curMax = 0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]==1){
                curMax +=1;
                if(i==nums.length-1){
                     if(curMax>max){
                        max = curMax;
                     }
                }
            }else{
                if(curMax>max){
                    max = curMax;
                }
                //当前连续输清零
                curMax = 0;
            }
        }
        return max;
    }
}
posted @ 2021-12-17 12:18  宗神一  阅读(29)  评论(0编辑  收藏  举报