forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_868.java
More file actions
23 lines (21 loc) · 695 Bytes
/
_868.java
File metadata and controls
23 lines (21 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _868 {
public static class Solution1 {
public int binaryGap(int N) {
String bin = Integer.toBinaryString(N);
List<Integer> oneIndexes = new ArrayList<>();
for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == '1') {
oneIndexes.add(i);
}
}
int maxGap = 0;
for (int i = 0; i < oneIndexes.size() - 1; i++) {
maxGap = Math.max(oneIndexes.get(i + 1) - oneIndexes.get(i), maxGap);
}
return maxGap;
}
}
}