-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (30 loc) · 1.03 KB
/
Solution.java
File metadata and controls
38 lines (30 loc) · 1.03 KB
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
package binarygap;
public class Solution {
public static int solution(int N) {
int temporaryLength = 0;
int finalLength = 0;
String binary = Integer.toBinaryString(N);
Status status = Status.SEARCHING;
char character;
for (int i = 0; i < binary.length(); i++) {
character = binary.charAt(i);
if (status == Status.SEARCHING && character == '0') {
temporaryLength++;
status = Status.COUNTING;
} else if (status == Status.COUNTING && character == '0') {
temporaryLength++;
} else if (status == Status.COUNTING && character == '1') {
if (finalLength < temporaryLength) finalLength = temporaryLength;
status = Status.SEARCHING;
temporaryLength = 0;
}
}
return finalLength;
}
enum Status {
SEARCHING, COUNTING
}
public static void main(String[] args) {
System.out.println(solution(1041));
}
}