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

来源:互联网 发布:linux执行命令的过程 编辑:程序博客网 时间:2024/05/16 23:46

解法一:

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;    }}


原创粉丝点击