-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path485.java
46 lines (34 loc) · 1 KB
/
485.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
46
//485
//https://leetcode.com/problems/max-consecutive-ones/
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
//write the int array to an String ArrayList
List <String> newarr = new ArrayList <>();
for (int e:nums){
newarr.add(Integer.toString(e));
}
/*
testing
for (String f:newarr){
System.out.print(f);
}
*/
//concat the item in newarr
String buffer =newarr.stream().collect(Collectors.joining(""));
int count = 0;
int maxCount = 0;
// Loop through the binary string to find the longest consecutive 1s
for (int i = 0; i < buffer.length(); i++) {
if (buffer.charAt(i) == '1') {
count++;
if (count > maxCount) {
maxCount = count;
}
} else {
count = 0;
}
}
// Print the result
return maxCount;
}
}