-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxConsecutive.java
45 lines (39 loc) · 1.08 KB
/
MaxConsecutive.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class MaxConsecutive {
public static void main(String[] args) {
int[] nums = {1, 1, 1, 1, 1, 0, 1};
System.out.println(findMaxConsecutiveOnes(nums));
}
//solution 1
public static int findMaxConsecutiveOnes(int[] nums) {
int cnt = 0, max = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
cnt++;
if (i == nums.length - 1 && nums[nums.length - 1] == 1) {
if (cnt > max) return cnt;
else return max;
}
} else if (cnt > max) {
max = cnt;
cnt = 0;
} else
cnt = 0;
}
return max;
}
//solution 2
/*
public static int findMaxConsecutiveOnes(int[] nums) {
int max = 0;
int cnt = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1)
cnt++;
else
cnt = 0;
if (cnt > max)
max = cnt;
}
return max;
}*/
}