-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201.py
32 lines (28 loc) · 813 Bytes
/
201.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
def rangeBitwiseAnd(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m == n:
return n
bin_m, bin_n = bin(m)[2:], bin(n)[2:]
max_len = max(len(bin_m), len(bin_n))
if len(bin_m)<len(bin_n):
bin_m = bin_m.zfill(max_len)
else:
bin_n = bin_n.zfill(max_len)
bin_n = bin_n[::-1]
bin_m = bin_m[::-1]
mask = ['1' for _ in range(32)]
for x in range(max_len):
if n-m >= 2**x:
mask[x] = '0'
else:
if bin_n[x] != bin_m[x]:
mask[x] = '0'
mask.reverse()
mask_int = int(''.join(mask), 2)
return m&mask_int
print(Solution().rangeBitwiseAnd(5,7))