Skip to content

Commit e6db924

Browse files
solves binary gap
1 parent 7028b0e commit e6db924

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings) | [![Java](assets/java.png)](src/BuddyStrings.java) |
234234
| 860 | [Lemonade Change](https://leetcode.com/problems/lemonade-change) | [![Java](assets/java.png)](src/LemonadeChange.java) |
235235
| 867 | [Transpose Matrix](https://leetcode.com/problems/transpose-matrix) | [![Java](assets/java.png)](src/TransposeMatrix.java) |
236-
| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap) | |
236+
| 868 | [Binary Gap](https://leetcode.com/problems/binary-gap) | [![Java](assets/java.png)](src/BinaryGap.java) |
237237
| 872 | [Leaf-Similar Trees](https://leetcode.com/problems/leaf-similar-trees) | |
238238
| 874 | [Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation) | |
239239
| 876 | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | |

src/BinaryGap.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class BinaryGap {
2+
public int binaryGap(int n) {
3+
int result = Integer.MIN_VALUE, index = 0, prev = -1;
4+
while (n > 0) {
5+
if ((n & 1) == 1) {
6+
if (prev == -1) prev = index;
7+
result = Math.max(result, index - prev);
8+
prev = index;
9+
}
10+
n >>= 1;
11+
index++;
12+
}
13+
return result;
14+
}
15+
}

0 commit comments

Comments
 (0)