File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 868. Binary Gap
3
+ * https://leetcode.com/problems/binary-gap/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given a positive integer n, find and return the longest distance between any two adjacent 1's
7
+ * in the binary representation of n. If there are no two adjacent 1's, return 0.
8
+ *
9
+ * Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance
10
+ * between two 1's is the absolute difference between their bit positions. For example, the two
11
+ * 1's in "1001" have a distance of 3.
12
+ */
13
+
14
+ /**
15
+ * @param {number } n
16
+ * @return {number }
17
+ */
18
+ var binaryGap = function ( n ) {
19
+ return Math . max ( 0 , ...n . toString ( 2 ) . split ( '1' ) . slice ( 1 , - 1 ) . map ( s => s . length + 1 ) ) ;
20
+ } ;
Original file line number Diff line number Diff line change 283
283
844|[ Backspace String Compare] ( ./0844-backspace-string-compare.js ) |Easy|
284
284
846|[ Hand of Straights] ( ./0846-hand-of-straights.js ) |Medium|
285
285
867|[ Transpose Matrix] ( ./0867-transpose-matrix.js ) |Easy|
286
+ 868|[ Binary Gap] ( ./0868-binary-gap.js ) |Easy|
286
287
872|[ Leaf-Similar Trees] ( ./0872-leaf-similar-trees.js ) |Easy|
287
288
876|[ Middle of the Linked List] ( ./0876-middle-of-the-linked-list.js ) |Easy|
288
289
884|[ Uncommon Words from Two Sentences] ( ./0884-uncommon-words-from-two-sentences.js ) |Easy|
You can’t perform that action at this time.
0 commit comments