-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path868.binary-gap.java
executable file
·35 lines (29 loc) · 1.01 KB
/
868.binary-gap.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BinaryGap{
public static void main(String[] args) {
Solution solution = new BinaryGap().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int binaryGap(int n) {
String n1 = Integer.toBinaryString(n); //shitty unoptimized way to do it :/
//TODO: LEARN BIT MANIPULATION!!!
int maxdist = 0;
//I made this negative one so that it misses the second
//if statement that actually calculates the max down there V
int last1 = -1;
for (int i = 0; i <n1.length(); i++) {
if (n1.charAt(i) == '1'){
if (last1>=0){
maxdist = Math.max(maxdist,i-last1);
}
last1 = i;
}
}
return maxdist;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}