leetcode 201. Bitwise AND of Numbers Range

来源:互联网 发布:js 获取input的value 编辑:程序博客网 时间:2024/06/14 07:07
class Solution(object):    def rangeBitwiseAnd(self, m, n):        """        :type m: int        :type n: int        :rtype: int        """        """        if m == n, then m is the answer        if m != n, then we look at the lsb        it is obvious that there exist an even number and        and and odd number in [m,n], which means the lsb of the        results must be 0 since the lsb of (even & odd) is 0        """        offset = 0        while m != n:            m >>= 1            n >>= 1            offset += 1        m <<= offset        return m

原创粉丝点击