1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class Ch1 {
public static void main(String[] args) {
System.out.println(consecutive_one(new int[]{0, 1, 1, 0, 1, 1, 1}));
System.out.println(consecutive_one(new int[]{0, 0, 0, 0}));
System.out.println(consecutive_one(new int[]{1, 0, 1, 0, 1, 1}));
}
private static int consecutive_one(int[] arr) {
int count = 0, res = 0;
for (int e : arr) {
if (e == 1) count++;
else {
res = Math.max(res, count);
count = 0;
}
}
return Math.max(count, res);
}
}
|