-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryGap.java
More file actions
31 lines (25 loc) · 826 Bytes
/
BinaryGap.java
File metadata and controls
31 lines (25 loc) · 826 Bytes
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
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int N) {
// write your code in Java SE 8
int result = 0, temp = 0;
String str =Integer.toBinaryString(N);
char[] ch = str.toCharArray();
Boolean counter = false;
for (int i = ch.length-1; i>=0;i--){
if(ch[i] == '1'){
counter = true;
if (temp > result){
result = temp;
}
temp=0;
}else if (counter==true){
temp++;
}
}
return result;
}
}