File tree Expand file tree Collapse file tree 3 files changed +99
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 3 files changed +99
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ Your ideas/fixes/algorithms are more than welcome!
41
41
| 896| [ Monotonic Array] ( https://leetcode.com/problems/monotonic-array/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_896.java ) | O(n) | O(1) | | Easy|
42
42
| 884| [ Uncommon Words from Two Sentences] ( https://leetcode.com/problems/uncommon-words-from-two-sentences/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_884.java ) | O(n) | O(k) | | Easy|
43
43
| 876| [ Middle of the Linked List] ( https://leetcode.com/problems/middle-of-the-linked-list/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_876.java ) | O(n) | O(1) | | Easy|
44
+ | 868| [ Binary Gap] ( https://leetcode.com/problems/binary-gap/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_868.java ) | O(n) | O(n) | | Easy|
44
45
| 867| [ Transpose Matrix] ( https://leetcode.com/problems/transpose-matrix/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_867.java ) | O(r* c) | O(r* c) | | Easy|
45
46
| 852| [ Peak Index in a Mountain Array] ( https://leetcode.com/problems/peak-index-in-a-mountain-array/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_852.java ) | O(n) | O(1) | | Easy|
46
47
| 844| [ Backspace String Compare] ( https://leetcode.com/problems/backspace-string-compare/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_844.java ) | O(n) | O(1) | | Easy|
Original file line number Diff line number Diff line change
1
+ package com .fishercoder .solutions ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
5
+
6
+ /**
7
+ * 868. Binary Gap
8
+ *
9
+ * Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.
10
+ *
11
+ * If there aren't two consecutive 1's, return 0.
12
+ *
13
+ * Example 1:
14
+ * Input: 22
15
+ * Output: 2
16
+ * Explanation:
17
+ * 22 in binary is 0b10110.
18
+ * In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
19
+ * The first consecutive pair of 1's have distance 2.
20
+ * The second consecutive pair of 1's have distance 1.
21
+ * The answer is the largest of these two distances, which is 2.
22
+ *
23
+ * Example 2:
24
+ * Input: 5
25
+ * Output: 2
26
+ * Explanation:
27
+ * 5 in binary is 0b101.
28
+ *
29
+ * Example 3:
30
+ * Input: 6
31
+ * Output: 1
32
+ * Explanation:
33
+ * 6 in binary is 0b110.
34
+ *
35
+ * Example 4:
36
+ * Input: 8
37
+ * Output: 0
38
+ * Explanation:
39
+ * 8 in binary is 0b1000.
40
+ * There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.
41
+ *
42
+ * Note:
43
+ * 1 <= N <= 10^9
44
+ */
45
+ public class _868 {
46
+ public static class Solution1 {
47
+ public int binaryGap (int N ) {
48
+ String bin = Integer .toBinaryString (N );
49
+ List <Integer > oneIndexes = new ArrayList <>();
50
+ for (int i = 0 ; i < bin .length (); i ++) {
51
+ if (bin .charAt (i ) == '1' ) {
52
+ oneIndexes .add (i );
53
+ }
54
+ }
55
+ int maxGap = 0 ;
56
+ for (int i = 0 ; i < oneIndexes .size () - 1 ; i ++) {
57
+ maxGap = Math .max (oneIndexes .get (i + 1 ) - oneIndexes .get (i ), maxGap );
58
+ }
59
+ return maxGap ;
60
+ }
61
+ }
62
+ }
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._868 ;
4
+ import org .junit .BeforeClass ;
5
+ import org .junit .Test ;
6
+
7
+ import static org .junit .Assert .assertEquals ;
8
+
9
+ public class _868Test {
10
+ private static _868 .Solution1 solution1 ;
11
+
12
+ @ BeforeClass
13
+ public static void setup () {
14
+ solution1 = new _868 .Solution1 ();
15
+ }
16
+
17
+ @ Test
18
+ public void test1 () {
19
+ assertEquals (2 , solution1 .binaryGap (22 ));
20
+ }
21
+
22
+ @ Test
23
+ public void test2 () {
24
+ assertEquals (2 , solution1 .binaryGap (5 ));
25
+ }
26
+
27
+ @ Test
28
+ public void test3 () {
29
+ assertEquals (1 , solution1 .binaryGap (6 ));
30
+ }
31
+
32
+ @ Test
33
+ public void test4 () {
34
+ assertEquals (0 , solution1 .binaryGap (8 ));
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments