Skip to content

Commit 1d1741e

Browse files
committed
Add solution #201
1 parent 6ee7b78 commit 1d1741e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
198|[House Robber](./0198-house-robber.js)|Medium|
175175
199|[Binary Tree Right Side View](./0199-binary-tree-right-side-view.js)|Medium|
176176
200|[Number of Islands](./0200-number-of-islands.js)|Medium|
177+
201|[Bitwise AND of Numbers Range](./0201-bitwise-and-of-numbers-range.js)|Medium|
177178
202|[Happy Number](./0202-happy-number.js)|Easy|
178179
203|[Remove Linked List Elements](./0203-remove-linked-list-elements.js)|Easy|
179180
204|[Count Primes](./0204-count-primes.js)|Medium|
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 201. Bitwise AND of Numbers Range
3+
* https://leetcode.com/problems/bitwise-and-of-numbers-range/
4+
* Difficulty: Medium
5+
*
6+
* Given two integers left and right that represent the range [left, right], return the
7+
* bitwise AND of all numbers in this range, inclusive.
8+
*/
9+
10+
/**
11+
* @param {number} left
12+
* @param {number} right
13+
* @return {number}
14+
*/
15+
var rangeBitwiseAnd = function(left, right) {
16+
let count = 0;
17+
while (left !== right) {
18+
left >>= 1;
19+
right >>= 1;
20+
count++;
21+
}
22+
return left << count;
23+
};

0 commit comments

Comments
 (0)