[LeetCode]201 数字范围按位与

题目描述

给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。

示例1:

输入: [5,7]
输出: 4

示例2:

输入: [0,1]
输出: 0

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def rangeBitwiseAnd(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
count = 0
while n != m:
count += 1
m >>= 1
n >>= 1
return m << count