比较简单,但解时候如果能一次通过更好
方案一
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;
}