Leetcode[201] Bitwise AND of Numbers Range
16 Nov 2015###Task1 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.
###Python
class Solution(object):
def rangeBitwiseAnd(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
pos = 0
while m != n:
m >>= 1
n >>= 1
pos += 1
return m << pos
###Points
由数据范围0 <= m <= n <= 2147483647可知,时间复杂度O(n)及以上的解法是不可接受的。
因此可以判断此题为数学题。
参考LeetCode Discuss链接:https://leetcode.com/discuss/32053/accepted-c-solution-with-simple-explanation
[m, n]范围的按位与的结果为m与n的公共“左边首部(left header)”