Skip to content

Commit fbe768a

Browse files
committed
feat: solve No.201
1 parent 796dcef commit fbe768a

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 201. Bitwise AND of Numbers Range
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Bit Manipulation.
5+
- Similar Questions: Longest Nice Subarray.
6+
7+
## Problem
8+
9+
Given two integers `left` and `right` that represent the range `[left, right]`, return **the bitwise AND of all numbers in this range, inclusive**.
10+
11+
 
12+
Example 1:
13+
14+
```
15+
Input: left = 5, right = 7
16+
Output: 4
17+
```
18+
19+
Example 2:
20+
21+
```
22+
Input: left = 0, right = 0
23+
Output: 0
24+
```
25+
26+
Example 3:
27+
28+
```
29+
Input: left = 1, right = 2147483647
30+
Output: 0
31+
```
32+
33+
 
34+
**Constraints:**
35+
36+
37+
38+
- `0 <= left <= right <= 231 - 1`
39+
40+
41+
42+
## Solution
43+
44+
```javascript
45+
/**
46+
* @param {number} left
47+
* @param {number} right
48+
* @return {number}
49+
*/
50+
var rangeBitwiseAnd = function(left, right) {
51+
var count = 0;
52+
while (left !== right) {
53+
left >>= 1;
54+
right >>= 1;
55+
count += 1;
56+
}
57+
return left << count;
58+
};
59+
```
60+
61+
**Explain:**
62+
63+
nope.
64+
65+
**Complexity:**
66+
67+
* Time complexity : O(1).
68+
* Space complexity : O(1).

0 commit comments

Comments
 (0)