
原题连接:
https://leetcode-cn.com/problems/max-consecutive-ones/
给定一个二进制数组, 计算其中最大连续 1 的个数。 示例: 输入:[1,1,0,1,1,1] 输出:3 解释:开头的两位和最后的三位都是连续 1 ,所以最大连续 1 的个数是 3.
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int max=0;
int temp=0;
for(int i:nums){
if(i==1){
temp++;
if(temp>max){
max=temp;
}
}
else{
temp=0;
}
}
return max;
}
}