Skip to content

Commit 97a63b9

Browse files
committed
Added Leetcode 201
1 parent f4a5547 commit 97a63b9

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int rangeBitwiseAnd(int m, int n) {
4+
int shift = 0;
5+
6+
// Shift both numbers to the right until they are equal
7+
while (m < n) {
8+
m >>= 1;
9+
n >>= 1;
10+
shift++;
11+
}
12+
13+
// Shift the common prefix back to its original position
14+
return m << shift;
15+
}
16+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int rangeBitwiseAnd(int m, int n) {
3+
int shift = 0;
4+
5+
// Shift both numbers to the right until they are equal
6+
while (m < n) {
7+
m >>= 1;
8+
n >>= 1;
9+
shift++;
10+
}
11+
12+
// Shift the common prefix back to its original position
13+
return m << shift;
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var rangeBitwiseAnd = function(m, n) {
2+
let shift = 0;
3+
4+
// Shift both numbers to the right until they are equal
5+
while (m < n) {
6+
m >>= 1;
7+
n >>= 1;
8+
shift++;
9+
}
10+
11+
// Shift the common prefix back to its original position
12+
return m << shift;
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def rangeBitwiseAnd(self, m: int, n: int) -> int:
3+
shift = 0
4+
5+
while m < n:
6+
m = m >> 1
7+
n = n >> 1
8+
shift += 1
9+
10+
return m << shift
11+
# Time: O(Bits)
12+
# Space: O(1)

0 commit comments

Comments
 (0)