Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d8a1041

Browse files
committed
Add solution #868
1 parent f718518 commit d8a1041

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

0868-binary-gap.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
844|[Backspace String Compare](./0844-backspace-string-compare.js)|Easy|
284284
846|[Hand of Straights](./0846-hand-of-straights.js)|Medium|
285285
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
286+
868|[Binary Gap](./0868-binary-gap.js)|Easy|
286287
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|
287288
876|[Middle of the Linked List](./0876-middle-of-the-linked-list.js)|Easy|
288289
884|[Uncommon Words from Two Sentences](./0884-uncommon-words-from-two-sentences.js)|Easy|

0 commit comments

Comments
 (0)