File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change 233
233
| 859 | [ Buddy Strings] ( https://leetcode.com/problems/buddy-strings ) | [ ![ Java] ( assets/java.png )] ( src/BuddyStrings.java ) |
234
234
| 860 | [ Lemonade Change] ( https://leetcode.com/problems/lemonade-change ) | [ ![ Java] ( assets/java.png )] ( src/LemonadeChange.java ) |
235
235
| 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 ) |
237
237
| 872 | [ Leaf-Similar Trees] ( https://leetcode.com/problems/leaf-similar-trees ) | |
238
238
| 874 | [ Walking Robot Simulation] ( https://leetcode.com/problems/walking-robot-simulation ) | |
239
239
| 876 | [ Middle of the Linked List] ( https://leetcode.com/problems/middle-of-the-linked-list ) | |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments