Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
code is as follow:
class Solution:
# @param m, an integer
# @param n, an integer
# @return an integer
def rangeBitwiseAnd(self, m, n):
i = 0
while m != n:
m = m >>1
n = n >> 1
i += 1
return m << i
本文介绍了一种针对给定范围 [m, n](0 ≤ m ≤ n ≤ 2147483647)内所有整数进行按位与运算的算法。通过迭代移位操作,该算法能够快速找到并返回整个范围内的按位与结果。以示例 [5, 7] 为例,最终输出为 4。此问题特别感谢 @amrsaqr 添加和创建所有测试案例。
632

被折叠的 条评论
为什么被折叠?



