485. Max Consecutive Ones

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int maxLength = 0;

for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
int j = i;
while (j < nums.length && nums[j] == 1) {
j++;
}
maxLength = Math.max(maxLength, j - i);
// now: j == nums.length or nums[j] == 0
i = j;
}
}

return maxLength;
}
}

References

485. Max Consecutive Ones