leetcode[Max Consecutive Ones]待整理多种解法

xiaoxiao2021-02-27  158

解法一:

public class Solution { public int findMaxConsecutiveOnes(int[] nums) { int max = 0; int count = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] == 1){ count++; } if(nums[i] == 0){//当为0时就清零即可,开启新的一段,并计算max max = Math.max(max, count); count = 0; } } //最后还要统计一次,因为防止后面直接结束了 max = Math.max(max, count); return max; } }

转载请注明原文地址: https://www.6miu.com/read-13410.html

最新回复(0)